Transcript Document
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
darker
Blue
reSize
Red
Green
Blue
Each color is mixture of amounts of
Red, Green, and Blue
( 200, 50 125 )
values in range: 0 … 255
Classes or Objects in Java
Illustrate with Rectangle object
Class definition of rectangle
including constructor, properties, methods
Test program that creates instance of rectangle
Class blueprint
Class Rectangle
{
int height , width ;
constructor
public Rectangle (int h, int w )
{
}
height = h;
width = w;
methods
int findArea ( )
{
return height * width ;
}
int getHght ( ) { return height }
}
instance variables
or properties
Test program
saved in file
TestRectangle
public class TestRectangle
{
constructor
public static void main ( String [ ] args )
{
instance r
of rectangle
Rectangle r ;
r = new Rectangle (2, 4) ;
int area = r.findArea ( );
System.out.println ( "Area is: " + area )
}
}
say:
r’s findArea
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
Rectangle s = new Rectangle (5, 10) ;
System.out.println ( "Area is: " + s.findArea ( ) );
}
}
Major Example
object
Program Design
Red
colorMe
Green
darker
Blue
reSize
Design object
GUI components
Recognize & handle events
Make Test program to create instances
GUI design
inherit properties
of frames
Build object on top of frame class
Put GUI elements in frame:
Add panel to frame
Add labels, textfields, & buttons
Make object listen for clicks on buttons
object
text fields
Red
Green
Blue
labels
colorMe
darker
reSize
buttons
panel
Asynchronously
called
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
Program Structure for Demo
Program Structure
import java.awt.*;
public class TestColorizer
{
public static void main (String[ ] args) { … }
}
class Colorizer extends
JFrame
implements ActionListener
{
private JTextField Num1 … ;
public Colorizer( ) { … }
public void actionPerformed(ActionEvent e) { … }
}
Class
JFrame
class Colorizer extends
implements ActionListener
{
Instance
variables
private JTextField Num1 … ;
public Colorizer( ) { … }
public void actionPerformed(ActionEvent e) { … }
constructor
}
Event-handler
JFrame
title
bar
X
Content Pane
Colorizer object
extends Jframe
X
Red
colorMe
Green
darker
Blue
reSize
JFrame
Title Bar
Content Pane
Panel
JLabel
JTextField
JButton
Constructor
public Colorizer ( )
{
objects
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
}
);
Constructor
un-named since not
referred to in eventhandler
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
}
system listens
for button click
);
named so can refer
to in event-handler
Class…again
class Colorizer extends
JFrame
implements ActionListener
{
instance
variables
private JTextField Num1 ;
private JPanel
p1
;
private JButton colorMe ;
public Colorizer( ) { … }
public void actionPerformed(ActionEvent e) { … }
}
Event-handler
method
signature
recognize event
source
public void actionPerformed (ActionEvent e)
{
if (e.getSource() == colorMe )
{
int R = Integer.parseInt ( Num1.getText( ) );
c = new Color (R, 100, 150);
p1.setBackground ( c );
}
}
Create color
Get & convert
number
Test program
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestColorizer
{
public static void main (String[ ] args)
{
Colorizer f = new Colorizer( );
create instance
of object
f.setSize (400,400);
f.setVisible(true);
}
}
use
inherited
methods
prototype
program
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestColorizer
{
public static void main (String[ ] args)
{
Colorizer f = new Colorizer( );
f.setSize (400,400);
f.setVisible(true);
} }
class Colorizer
extends JFrame
{
private JTextField Num1 ;
private JPanel
p1
;
private JButton
colorMe;
private Color
c
;
implements ActionListener
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
);
public void actionPerformed (ActionEvent e) {
if (e.getSource() == colorMe )
Integer.parseInt ( Num1.getText();)
{ int R =
= new Color (R, 100, 150);
} cp1.setBackground
( c );
}
Outline
Demo Colorizer Example
compilation
execution
RGB representation
buttons
darken-button
Colorizer class
main program
TextPad
panel color
resize-button
Classes in Java
Rectangle example
properties or instance variables
instances of class
constructor for class
Outline
Colorizer Class Design
program structure
constructor
GUI setup
event-handler
constructor
panel
event-handler
recognize event-source
labels
grab text
textFields
make Color
buttons
reset background color
actionListeners
reset window size
instance variables
declarations
Terms and Concepts
TextPad
Classes
Objects
Methods
Instances of objects
Instance variables
Event-driven programming
Asynchronous
Inheriting properties & methods
extend class
Constructors
Method signature
JFrames
GUI components
JTextFields
JButtons
JLabels
JPanels
JPanel’s add method
this notation
ActionEvent
ActionEvent’e getSource() method
JFrame’s setSize method
JFrame’s setVisible method
import statements
Multiple instances
Color objects
RGB representation
JPanel’s setBackground ( c) method
Test program
Program structure