Chapter 16: Event-Driven Programming

advertisement
Computer Science Notes
Chapter 16
Page 1 of 5
Chapter 16: Event-Driven Programming
These notes are meant to accompany Introduction to Java Programming: Brief Version, eighth edition by Y.
Daniel Lang.
Big Skills:
1. jjj
Book’s Statement of Skills:
1. To describe events, event sources, and event classes.
2. To declare listener classes, register listener objects with the source object, and write the code to handle
events.
3. To declare listener classes using inner classes.
4. To declare listener classes using anonymous inner classes.
5. To write programs to deal with ActionEvent.
6. To write programs to deal with WindowEvent.
7. To simplify code for listener classes using listener interface adapters.
8. To write programs to deal with MouseEvent.
9. To write programs to deal with KeyEvent.
10. To use the javax.swing.Timer class to deal with animations.
Section 16.1: Introduction
 Event-driven programming involves writing code that is executed when an event occurs (like a button
click), and not in procedural order.
Section 16.2: Event and Event Source
 An event is a signal to a program that something has happened.
o Like mouse movements, button clicks, keystrokes, or internal program activities like a timer
 The source object (or source component) is the component on which the event is fired or generated.
o The root class of all event classes is: java.util.EventObject.
o See image on next page for subclasses of EventObject.
o The getSource() method of the EventObject class identifies the source object of an
event.
o If a component can fire an event, then any subclass of the component can, also.
Computer Science Notes
Chapter 16
Page 2 of 5
Computer Science Notes
Chapter 16
Page 3 of 5
Section 16.3: Listeners, Registrations, and Handling Events
 A source object fires an event, and a listener object handles the event.
 To be a listener, the following two things are needed:
o The listener must be an instance of the corresponding event-listener interface. This ensures it has
a method to handle the event. Java has an interface for every type of GUI event (pattern:
ActionListener is the event interface for the ActionEvent event…). Methods prescribed
in the interface to process an event are called handlers.
o The listener object must be registered by the source object (pattern: addActionListener()
is the name of the method to register an ActionEvent event…).
 Here is an old example from Chapter 11: HandleEvent.java
o The following line creates a source object:
JButton jbtOK = new JButton("OK");
o The following line creates a listener object:
OKListenerClass listener1 = new OKListenerClass();
o The following line registers a listener:
jbtOK.addActionListener(listener1);
import javax.swing.*;
import java.awt.event.*;
/**
* The HandleEvent class is a GUI class that displays two buttons.
*/
public class HandleEvent extends JFrame
{
/**
* Construct a HandleEvent object with two buttons.
*/
public HandleEvent()
{
// Create two buttons
JButton jbtOK = new JButton("OK");
JButton jbtCancel = new JButton("Cancel");
// Create a panel to hold buttons
JPanel panel = new JPanel();
panel.add(jbtOK);
panel.add(jbtCancel);
add(panel); // Add panel to the frame
// Register listeners
OKListenerClass listener1 = new OKListenerClass();
CancelListenerClass listener2 = new CancelListenerClass();
jbtOK.addActionListener(listener1);
jbtCancel.addActionListener(listener2);
}//end constructor HandleEvent()
/**
* Creates an instance of the HandleEvent class and displays the GUI.
* @param args is not used
*/
public static void main(String[] args)
{
JFrame frame = new HandleEvent();
frame.setTitle("Handle Event");
Computer Science Notes
Chapter 16
Page 4 of 5
frame.setSize(200, 150);
frame.setLocation(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}//end method main(String[])
}//end class HandleEvent
/**
* The OKListenerClass is a listener for the "OK" button of the HandleEvent class.
* The OKListenerClass implements the ActionListener interface for the "OK" button
* of the HandleEvent class.
*/
class OKListenerClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("OK button clicked");
}//end method actionPerformed(ActionEvent)
}//end inner class CancelListenerClass
/**
* The CancelListenerClass is a listener for the "Cancel" button of the HandleEvent
class.
* The CancelListenerClass implements the ActionListener interface for the "Cancel"
button
* of the HandleEvent class.
*/
class CancelListenerClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("Cancel button clicked");
}//end method actionPerformed(ActionEvent)
}//end inner class CancelListenerClass

See www.cs.armstrong.edu/liang/intro7e/book/SimpleEventDemo.java
Section 16.3.1: Inner Class Listeners
 An inner class (or nested class) is a class defined within the scope of another class. Inner classes are
frequently used to define listener objects in GUI classes, because those listener objects are created
specifically for the GUI component.
 See www.cs.armstrong.edu/liang/intro7e/book/SimpleEventDemoInnerClass.java
Section 16.3.2: Anonymous Class Listeners
 An anonymous inner class is an inner class without a name that is defined “on the spot” when
registering a listener.
 Syntax:
new SuperClassName / InterfaceName() {//methods…}}
 See www.cs.armstrong.edu/liang/intro7e/book/AnonymousListenerDemo.java
Computer Science Notes
Chapter 16
Page 5 of 5
Section 16.3.3: Example: Enlarging or Shrinking a Circle
 See www.cs.armstrong.edu/liang/intro7e/book/ControlBall.java
Section 16.3.4: Handling Window Events
 See www.cs.armstrong.edu/liang/intro7e/book/TestWindowEvent.java
Section 16.3.5: Listener Interface Adapters
 A convenience adapter is a support class provided in Java with default implementations (i.e., empty
bodies) for all of the methods in every listener interface, so you do not have to implement all the
methods yourself.
 See www.cs.armstrong.edu/liang/intro7e/book/AdapterDemo.java
Section 16.4: Mouse Events
 A mouse event is fired whenever a mouse is pressed, released, clicked, moved, or dragged on a
component.
o MouseListener and MouseMotionListener are the listener interfaces for mouse events.
Section 16.4.1: Example: Moving a Message on a Panel Using a Mouse
 See www.cs.armstrong.edu/liang/intro7e/book/MoveMessageDemo.java
Section 16.5: Key Events
 A key event is fired whenever a key on the keyboard is pressed, released, or typed on a component.
o The KeyEvent class describes the nature of the event.
o The KeyListener interface is the listener that handles key events.
 See www.cs.armstrong.edu/liang/intro7e/book/KeyEventDemo.java
Section 16.6: Animation Using the Timer Class
 The javax.swing.Timer class is a source component that fires an ActionEvent at a predefined
rate.
 See www.cs.armstrong.edu/liang/intro7e/book/AnimationDemo.java
 See www.cs.armstrong.edu/liang/intro7e/book/ClockAnimation.java
Download