Introduction to Java Classes, events, GUI’s
Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven programming works How classes inherit methods Overall program structure
Major Example
Red colorMe GreenBlue darkerreSize
RedGreenBlue Each color is mixture of amounts of Red, Green, and Blue ( 200, ) values in range: 0 … 255
Illustrate with Rectangle object Class definition of rectangle including constructor, properties, methods 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 ; instance variables or properties constructor Class blueprint 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 ) Test program instance r of rectangle saved in file TestRectangle 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) ;
Major Example
Red colorMe GreenBlue darkerreSize object Program Design
Design object GUI components Recognize & handle events Make Test program to create instances
GUI design Build object on top of frame class Add panel to frame Put GUI elements in frame: Add labels, textfields, & buttons Make object listen for clicks on buttons inherit properties of frames
Red colorMe GreenBlue darkerreSize object labelsbuttons text fields panel
Event-handler design Recognize source of event Take appropriate action - grab data from textfield - convert data to integer - use value to make RGB color - RGB: ( r, g, b ) - change color of panel Asynchronously called
Program Structure for Demo
import java.awt.*; public class TestColorizer { } public static void main (String[ ] args) { … } class Colorizer { }{ } public void actionPerformed(ActionEvent e) { … } public Colorizer( ) { … } private JTextField Num1 … ; implements ActionListener extends JFrame Program Structure
class Colorizer { }{ } implements ActionListener extends JFrame private JTextField Num1 … ; Instance variables public Colorizer( ) { … } constructor public void actionPerformed(ActionEvent e) { … } Event-handler Class
Content Pane X title bar JFrame
X Red colorMe GreenBlue darkerreSize Colorizer object extends Jframe
JFrame Title Bar Content PanePanel JTextFieldJLabelJButton
public Colorizer ( ) { } p1 = new JPanel ( ); this.getContentPane().add(p1); p1.add(new JLabel(“Red")); p1.add(Num1 = new JTextField(3 )); p1.add(colorMe = new JButton("ColorMe")); colorMe.addActionListener(this ); objects Constructor
public Colorizer ( ) { } p1 = new JPanel ( ); this.getContentPane().add(p1); p1.add(new JLabel(“Red")); p1.add(Num1 = new JTextField(3 )); p1.add(colorMe = new JButton("ColorMe")); colorMe.addActionListener(this ); named so can refer to in event-handler Constructor un-named since not referred to in event- handler system listens for button click
class Colorizer { }{ } public Colorizer( ) { … } private JTextField Num1 ; implements ActionListener extends JFrame private JButton colorMe ; private JPanel p1 ; public void actionPerformed(ActionEvent e) { … } instance variables Class…again
if (e.getSource() == colorMe ) { } public void actionPerformed (ActionEvent e) { } p1.setBackground ( c ); Integer.parseInt ( );int R =Num1.getText( ) recognize event source Get & convert number c = new Color (R, 100, 150); Create color Event-handler method signature
import java.awt.*; public class TestColorizer { } public static void main (String[ ] args) { } Colorizer f = new Colorizer( ); f.setSize (400,400); f.setVisible(true); import javax.swing.*; import java.awt.event.*; create instance of object use inherited methods Test program
public class TestColorizer { } public static void main (String[ ] args) { } Colorizer f = new Colorizer( ); f.setSize (400,400); f.setVisible(true); import java.awt.*; import javax.swing.*; import java.awt.event.*; { }{ } class Colorizer implements ActionListener extends JFrame if (e.getSource() == colorMe ) { } c = new Color (R, 100, 150); p1.setBackground ( c ); Integer.parseInt ( );int R =Num1.getText( ) public void actionPerformed (ActionEvent e) { } private JTextField Num1 ; private JButton colorMe; private JPanel p1 ; private Color c ; p1 = new JPanel ( ); this.getContentPane().add(p1); p1.add(new JLabel(“Red")); p1.add(Num1 = new JTextField(3 )); p1.add(colorMe = new JButton("ColorMe")); colorMe.addActionListener(this ); public Colorizer ( ) { } prototype program
Outline Classes in Java Rectangle example propertiesor instance variables instances of class constructor for class Demo Colorizer Example compilationexecution RGB representationpanel color buttonsdarken-buttonresize-button Colorizer class main program TextPad
Colorizer Class Design program structure constructor event-handler GUI setup constructor recognize event-source grab text make Color reset background color reset window size event-handler instance variables declarations labels textFields buttons panel actionListeners Outline
TextPad Multiple instances Terms and Concepts GUI components JTextFields JButtons JLabels Event-driven programming Asynchronous Inheriting properties & methods Instance variables 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 Test program Program structure JPanel’s setBackground ( c) method