Basic Java – Interface design
Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven programming works How classes inherit methods Overall program structure
Illustrate with Rectangle object Class definition of rectangle including constructor, properties, methods Driver or Test program that creates instance of rectangle Classes or Objects in Java
class Rectangle int height, width ; { } public Rectangle (int h, int w ) { } height = h; width = w; int findArea ( ) { } return height * width ; properties or instance variables constructor class= blueprint for object int getHght ( ) { return height } methods
public class TestRectangle { } public static void main ( String [ ] args ) { } Rectangle r ; r = new Rectangle (2, 4) ; int area = r.findArea ( ); System.out.println ( "Area is: " + area ) Driver or Test program instance r of rectangle saved in file TestRectangle.java say: r’s findArea constructor
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 ) ; another instance System.out.println ( "Area is: " + s.findArea ( ) ); Rectangle s = new Rectangle (5, 10) ;
Defining & Executing a window-like object
public class X extends JFrame { } import javax.swing.*; import java.awt.*; import java.awt.event.*; X is a JFrame...and maybe more Import Java class libraries to be used currently... just a shell
public class X extends JFrame { } import javax.swing.*; import java.awt.*; import java.awt.event.*; needs a main program to run class X file X.java Compile: ctrl-1 [ in TextPad ] Execute: ctrl-2 " " not this class !
Empty JFrame
import javax.swing.* ; import java.awt.* ; public class TestX { } public static void main (String [ ] args ) { X m = new X() ; m.setVisible(true) ; m.setSize(200, 200) ; } // MyInput.readString(); main program JFrame methods For dos IO or stalling program - requires MyInput class creates instance m like: int k = 3; main
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) frame methods - setVisible(true) & setSize(200, 200) create instance - X m = new X( ) ; DOS IO - MyInput.readString ( ) ;
Calculator example inputsmultiply
Plan of Attack for calculator object Containers: a canvas JPanels Make input fields & place in panelJTextFields Make buttons trigger actionsJButtons Define handler for button eventsactionPerformed Acquire, convert and store field data Containers: content pane getContentPane ( ) Structure Java
1. JFrame2. ContentPane 3. JPanel button JButton hello 4. JTextField
JFrame Title Bar Content PanePanel JTextFieldJLabelJButton
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Y extends JFrame { } public Y() { } JPanel p ; Declare panel p Create panel p Add p to pane Panels can contain GUI objects like buttons panels Constructor Y() p = new JPanel(); this.getContentPane().add(p) ;
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Y extends JFrame implements ActionListener { } JPanel p; public Y() { p = new JPanel(); this.getContentPane().add(p); } declare fields & buttons create fields & add to p JTextField n1, n2, n3; JButton b1,b2; n1=new JTextField(10); p.add(n1); n2=new JTextField(10); p.add(n2); n3=new JTextField(10); p.add(n3); create buttons & add to p b1=new JButton("+"); p.add(b1); b2=new JButton("*"); p.add(b2); Fields & Buttons
Events import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Y extends JFrame { } 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); } public void actionPerformed (ActionEvent bert) { } JPanel p ; JTextField n1, n2, n3 ; JButton b1, b2 ; make buttons listen for events template for event handler says class has events b1.addActionListener(this); b2.addActionListener(this); implements ActionListener
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Y extends JFrame implements ActionListener { } 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); } public void actionPerformed (ActionEvent bert ) { } JPanel p ; JTextField n1, n2, n3 ; JButton b1, b2 ; event handler template 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 ) ); + or - depending on source of event convert & back/store get/convert data
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Y extends JFrame implements ActionListener { } 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); } 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) ); } JPanel p ; JTextField n1, n2, n3 ; JButton b1, b2 ; class Y definition Global declarations Constructor Y ( ) Event-handler
TextPad Multiple instances Terms and Concepts GUI components JTextFields JButtons JLabels Event-driven programming Asynchronous Inheriting properties & methods Constructors Method signature Classes Objects Methods Instances of objects JFrames import statements extend class JFrame’s setSize method JFrame’s setVisible method JPanels JPanel’s add method this notation ActionEvent ActionEvent’e getSource() method Color objects RGB representation dos IO – MyInput.readString ( ) Program structure JPanel’s setBackground ( c) method