1 / 22

Basic Java – Interface design

Basic Java – Interface design. Understand:. How to define classes and objects. How to create a GUI interface . How event-driven programming works. How classes inherit methods. Overall program structure. How to use TextPad for Java. Classes or Objects in Java.

Download Presentation

Basic Java – Interface design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Basic Java – Interface design

  2. Understand: How to define classes and objects How to create a GUI interface How event-driven programming works How classes inherit methods Overall program structure How to use TextPad for Java

  3. Classes or Objects in Java Illustrate with Rectangle object Class definition of rectangle including constructor, properties, methods Driver or Test program that creates instance of rectangle

  4. properties or instance variables constructor methods class= blueprint for object class Rectangle { int height , width ; public Rectangle (int h, int w ) { } height = h; width = w; int findArea ( ) { } return height * width ; int getHght ( ) { return height } }

  5. saved in file TestRectangle.java constructor instance r of rectangle say: r’s findArea Driver or Test program publicclassTestRectangle { public static void main ( String [ ] args ) { } Rectangle r ; r = new Rectangle (2, 4) ; int area = r.findArea ( ); System.out.println ( "Area is: " + area ) }

  6. another instance public class Rectangle { public static void main ( String [ ] args ) { } Rectangle r ; r = new Rectangle (2, 4) ; int area = r.findArea ( ); System.out.println ( "Area is: " + area ) ; Rectangle s = new Rectangle (5, 10) ; System.out.println ( "Area is: " + s.findArea ( ) ); }

  7. Defining & Executing a window-like object

  8. Import Java class libraries to be used import javax.swing.* ; import java.awt.* ; import java.awt.event.* ; public class X extends JFrame { } X is a JFrame ...and maybe more currently... just a shell

  9. import javax.swing.* ; import java.awt.* ; import java.awt.event.* ; class X file X.java public class X extends JFrame { } needs a main program to run Compile: ctrl-1 [ in TextPad ] Execute: ctrl-2 " " not this class !

  10. Empty JFrame

  11. main import javax.swing.* ; import java.awt.* ; public class TestX { } main program public static void main (String [ ] args ) { X m = new X() ; m.setVisible(true) ; m.setSize(200, 200) ; } creates instance m like: int k = 3; JFrame methods // MyInput.readString(); For dos IO or stalling program - requires MyInput class

  12. Summary class X - blueprint for object import - provides methods for object extends JFrame - X can use [inherits] JFrame methods naming - class X in file X.java TextPad - can compile separately; but run main Main - public static void main (String [ ] args) create instance - X m = new X( ) ; frame methods - setVisible(true) & setSize(200, 200) DOS IO - MyInput.readString ( ) ;

  13. Calculator example inputs multiply

  14. Plan of Attack for calculator object Structure Java Containers: content pane getContentPane ( ) Containers: a canvas JPanels Make input fields & place in panel JTextFields Make buttons trigger actions JButtons Define handler for button events actionPerformed Acquire, convert and store field data

  15. 2. ContentPane 1. JFrame 3. JPanel hello button 4. JTextField JButton

  16. Title Bar Content Pane Panel JLabel JTextField JButton JFrame

  17. panels import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Y extends JFrame { } Constructor Y() JPanel p ; Declare panel p public Y() { } p = new JPanel() ; Create panel p this.getContentPane().add(p) ; Add p to pane Panels can contain GUI objects like buttons

  18. import javax.swing.*; import java.awt.*; import java.awt.event.*; Fields & Buttons public class Y extends JFrame implements ActionListener { } JPanel p; JTextField n1, n2, n3; JButton b1,b2; declare fields & buttons public Y() { p = new JPanel(); this.getContentPane().add(p); } create fields & add to p n1=new JTextField(10); p.add(n1); n2=new JTextField(10); p.add(n2); n3=new JTextField(10); p.add(n3); b1=new JButton("+"); p.add(b1); b2=new JButton("*"); p.add(b2); create buttons & add to p

  19. Events import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Y extends JFrame { } says class has events implements ActionListener JPanel p ; JTextField n1, n2, n3 ; JButton b1, b2 ; public Y() { p = new JPanel() ; this.getContentPane().add(p) ; n1=new JTextField(10); p.add(n1); n2=new JTextField(10); p.add(n2); n3=new JTextField(10); p.add(n3); b1=new JButton("+") ; p.add(b1); b2=new JButton("*") ; p.add(b2); } make buttons listen for events b1.addActionListener(this); b2.addActionListener(this); public void actionPerformed (ActionEvent bert) { } template for event handler

  20. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Y extends JFrame implements ActionListener { } event handler template JPanel p ; JTextField n1, n2, n3 ; JButton b1, b2 ; public Y() { p = new JPanel() ; this.getContentPane().add(p) ; n1=new JTextField(10); p.add(n1); n2=new JTextField(10); p.add(n2); n3=new JTextField(10); p.add(n3); b1=new JButton("+") ; p.add(b1); b2=new JButton("*") ; p.add(b2); b1.addActionListener(this); b2.addActionListener(this); } get/convert data public void actionPerformed (ActionEvent bert) { } + or - depending on source of event int num1=Integer.parseInt(n1.getText()) ; int num2=Integer.parseInt(n2.getText()) ; int num3=0; if (bert.getSource()==b1) { num3=num1 + num2 ; } if (bert.getSource ()==b2) { num3=num1 * num2 ; } n3.setText ( String.valueOf ( num3 ) ) ; convert & back/store

  21. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Y extends JFrame implements ActionListener { } class Y definition JPanel p ; JTextField n1, n2, n3 ; JButton b1, b2 ; Global declarations public Y() { p = new JPanel() ; this.getContentPane().add(p) ; n1=new JTextField(10); p.add(n1); n2=new JTextField(10); p.add(n2); n3=new JTextField(10); p.add(n3); b1=new JButton("+") ; p.add(b1); b2=new JButton("*") ; p.add(b2); b1.addActionListener(this); b2.addActionListener(this); } Constructor Y ( ) public void actionPerformed (ActionEvent bert) { int num1=Integer.parseInt(n1.getText()) ; int num2=Integer.parseInt(n2.getText()) ; int num3=0; if (bert.getSource()==b1){num3=num1 + num2 ; } if (bert.getSource()==b2){num3=num1 * num2 ; } n3.setText ( String.valueOf(num3) ) ; } Event-handler

  22. Terms and Concepts JPanels TextPad JPanel’s add method Classes this notation Objects ActionEvent Methods ActionEvent’e getSource() method Instances of objects JFrame’s setSize method Event-driven programming JFrame’s setVisible method Asynchronous import statements Inheriting properties & methods Multiple instances extend class Program structure Constructors Method signature JFrames GUI components Color objects RGB representation JTextFields JPanel’s setBackground ( c) method JButtons JLabels dos IO – MyInput.readString ( )

More Related