introduction to java classes, events, gui’s. understand: how to use textpad how to define a class...
TRANSCRIPT
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
Green Blue
darker reSize
Red Green Blue
Each color is mixture of amounts of Red, Green, and Blue
( 200, 50 125 )
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 variablesor 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 rof 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 ) ;anotherinstance
System.out.println ( "Area is: " + s.findArea ( ) );
Rectangle s = new Rectangle (5, 10) ;
Major Example
Red
colorMe
Green Blue
darker reSize
objectProgram Design
Design object
GUI componentsRecognize & 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 propertiesof frames
Red
colorMe
Green Blue
darker reSize
object
labels
buttons
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
Asynchronouslycalled
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 ActionListenerextends JFrame
Program Structure
class Colorizer{
}
implements ActionListenerextends JFrame
private JTextField Num1 … ;
Instancevariables
public Colorizer( ) { … }
constructor
public void actionPerformed(ActionEvent e) { … }
Event-handler
Class
Content Pane
X
titlebar
JFrame
X
Red
colorMe
Green Blue
darker reSize
Colorizer objectextends Jframe
JFrame
Title Bar Content Pane
Panel
JTextFieldJLabel JButton
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 listensfor button click
class Colorizer
{
}
public Colorizer( ) { … }
private JTextField Num1 ;
implements ActionListenerextends JFrame
private JButton colorMe ;
private JPanel p1 ;
public void actionPerformed(ActionEvent e) { … }
instancevariables
Class…again
if (e.getSource() == colorMe ){
}
public void actionPerformed (ActionEvent e) {
}
p1.setBackground ( c );
Integer.parseInt ( );int R = Num1.getText( )
recognize eventsource
Get & convertnumber
c = new Color (R, 100, 150);
Create color
Event-handlermethodsignature
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 instanceof object
use inheritedmethods
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 ActionListenerextends 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 ( ) {
}
prototypeprogram
Outline
Classes in Java
Rectangle exampleproperties or instance variablesinstances of classconstructor for class
Demo Colorizer Example
compilation executionRGB representation panel colorbuttons darken-button resize-buttonColorizer classmain 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
declarationslabels
textFields
buttons
panel
actionListeners
Outline
TextPad
Multiple instances
Terms and Concepts
GUI componentsJTextFields
JButtons
JLabels
Event-driven programming
AsynchronousInheriting properties & methods
Instance variables
Constructors
Method signature
ClassesObjectsMethodsInstances of objects
JFrames
import statements
extend class
JFrame’s setSize methodJFrame’s setVisible method
JPanelsJPanel’s add methodthis notationActionEvent
ActionEvent’e getSource() method
Color objectsRGB representation
Test programProgram structure
JPanel’s setBackground ( c) method