230 likes | 463 Views
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.
E N D
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 Illustrate with Rectangle object Class definition of rectangle including constructor, properties, methods Driver or Test program that creates instance of rectangle
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 } }
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 ) }
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 ( ) ); }
Defining & Executing a window-like object
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
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 !
Empty JFrame
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
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 ( ) ;
Calculator example inputs multiply
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
2. ContentPane 1. JFrame 3. JPanel hello button 4. JTextField JButton
Title Bar Content Pane Panel JLabel JTextField JButton JFrame
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
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
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
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
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
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 ( )