? 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 1
Chapter 13
GUI Objects and Event-Driven Programming
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 2
Chapter 13 Objectives
After you have read and studied this chapter,you should be able to
Write GUI application programs using Frame,Dialog,and
Button objects from the java.awt package.
Write GUI application programs with menus using Menu,
MenuItem,and MenuBar objects from the java.awt
package.
Write event-driven programs using Java’s delegation-
based event model.
Write GUI application programs that process mouse
events.
Understand how the SketchPad class introduced in
Chapter 1 is implemented.
Run applets as applications.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 3
GUI Objects
MenuBar
MenuItem
TextField
Label
Button
Frame
Dialog
Menu
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 4
Interacting with Buttons
Place two buttons labeled OK and CANCEL on a
frame,
Change the frame title when either button is clicked.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 5
Creating a Frame Object
import java.awt.*;
class MyFirstFrame extends Frame
{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 200;
private static final int FRAME_X_ORIGIN = 150;
private static final int FRAME_Y_ORIGIN = 250;
public MyFirstFrame( )
{
setSize ( FRAME_WIDTH,FRAME_HEIGHT );
setResizable ( false );
setTitle ( "Program MyFirstFrame" );
setLocation ( FRAME_X_ORIGIN,FRAME_Y_ORIGIN );
}
}
150
250
300
200
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 6
Placing a Button on a Frame
setLayout( null );
okButton = new Button(,OK” );
okButton.setBounds( 100,150,60,30);
add( oKButton );
100
150
60
30
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 7
Handling Action Events - ActionListener
import java.awt.event.*;
class MyFirstFrame extends Frame
implements ActionListener
{
.,,
}
public MyFirstFrame ( )
{
.,,
cancelButton.addActionListener( this );
okButton.addActionListener( this );
.,,
}
Declare MyFirstFrame as
an ActionListener.
Register MyFirstFrame
as the action listener of
both buttons.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 8
Handling Action Events - actionPerformed
public void actionPerformed( ActionEvent event )
{
Button clickedButton = (Button) event.getSource();
if (clickedButton == cancelButton) {
setTitle( "You clicked CANCEL" );
}
else { //the event source is okButton
setTitle( "You clicked OK" );
}
}
Define the actionPerformed method in the class that
implements the ActionListener interface,
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 9
Handling Window Events
class ProgramTerminator implements WindowListener
{
public void windowClosing( WindowEvent event )
{
System.exit(0);
}
public void windowActivated ( WindowEvent event ) { }
public void windowClosed ( WindowEvent event ) { }
public void windowDeactivated( WindowEvent event ) { }
public void windowDeiconified( WindowEvent event ) { }
public void windowIconified ( WindowEvent event ) { }
public void windowOpened ( WindowEvent event ) { }
}
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 10
Adding Menus to a Frame
Menu fileMenu = new Menu(,File” );
.,,
MenuBar menuBar = new MenuBar( );
setMenuBar( menuBar );
menuBar.add( fileMenu );
Create a MenuItem
object,associate an
action listener to it,and
add it to the Menu object.
Create a MenuBar object
and add it to a frame,Add
Menu objects to it.
MenuItem menuItem = new MenuItem(,Open…” );
item.addActionListener( this );
fileMenu.add( item );
.,,
Create a Menu object,
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 11
Handling Menu Selection
public void actionPerformed( ActionEvent event )
{
String menuName;
menuName = event.getActionCommand();
if ( menuName.equals("Quit") ) {
System.exit(0);
}
else {
response.setText("Menu item '" + menuName
+ "' is selected");
}
}
Here we simply display the label of the selected
menu,In a more realistic program,call
appropriate private methods to process the
menu selection,
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 12
Mouse Events - MouseListener
class TrackMouseFrame extends Frame
implements MouseListener
{
.,,
}
public TrackMouseFrame ( )
{
.,,
addMouseListener( this );
.,,
}
Declare TrackMouse
Frame as an
MouseListener,This
frame is also a
mouse event source.
Register itself as the
mouse listener.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 13
Mouse Events - mouseClicked
public void mouseClicked( MouseEvent event )
{
int x,y;
x = event.getX(); //return the x and y coordinates
y = event.getY(); //of a mouse click point
outputBox.printLine("[" + x + "," + y + "]");
}
public void mouseEntered ( MouseEvent event ) { }
public void mouseExited ( MouseEvent event ) { }
public void mousePressed ( MouseEvent event ) { }
public void mouseReleased( MouseEvent event ) { }
Define the mouseClicked method in the class that
implements the MouseListener interface,Other methods
have no method body,i.e.,they are do-nothing methods.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 14
Mouse Move Events - MouseMotionListener
class SketchPad extends MainWindow
implements MouseListener,
MouseMotionListener
{
.,,
}
public SketchPad ( )
{
.,,
addMouseMotionListener( this );
.,,
}
Implement the
MouseMotionListener
to process mouse
movements.
Register itself as the
mouse motion listener.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 15
Mouse Motion Events - mouseDragged
public void mouseDragged ( MouseEvent event )
{
int x = event.getX();
int y = event.getY();
if ( !event.isMetaDown() ) {
//don’t process the right button drag
Graphics g = getGraphics();
g.drawLine(last_x,last_y,x,y);
g.dispose();
last_x = x;
last_y = y;
}
}
public void mouseMoved ( MouseEvent event ) { }
Define the mouseDragged method in the class that
implements the MouseMotionListener interface,
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 16
Other GUI Objects
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 17
Running Applets as Applications
An Applet is a Component that can be added to a Frame,
You can run an applet as an application simply by placing it on a Frame.
import java.awt.*;
class GreetingAppletMain
{
public static void main (String[] args)
{
Frame myFrame;
myFrame = new Frame("Greeting Applet in Frame");
myFrame.setSize(300,270);
myFrame.addWindowListener( new ProgramTerminator() );
myFrame.add( new GreetingApplet() );
myFrame.setVisible( true );
}
}
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 18
Sample Class,A Simple Calculator
Problem Statement
Write a calculator that has two text fields to enter left and
right operands and five operator buttons for add,subtract,
divide,multiply,and clear,Clicking the CLEAR button clears
both text fields,The result of the operation is displayed in the
top text field,The user can continue the calculation by
entering the next number in the bottom text field and clicking
the desired operator.
Capabilities
Two TextField objects to enter the left and right
operands.
Four Button objects for the operators.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 19
Sample Interaction
Get the sum of 12.54 and 7.46.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 13 - 20
Development Steps
1,Start with a class skeleton,Define the necessary data
members and constructors,Add any other methods
necessary to implement the constructors.
2,Implement the actionPerformed method,Implement the
private methods clearEntries and compute,For this step,
assume the input is valid.
3,Implement methods necessary to check for invalid input
data.
4,Finalize the code and look for improvement.
The End