ptg7068951
WHAT YOU’LL LEARN IN
THIS HOUR:
.Making your programs
aware of events
.Setting up a component so
it can cause events
.Ignoring some components
.Finding out where events
end up in a program
.Storing information in the
interface
.Converting values stored in
text fields
The graphical user interface (GUI) you developed during the past two
hours can run on its own. Users can click buttons, fill text fields with text,
and resize the window. Sooner or later, even the least discriminating user
is going to be left wanting more. The GUI that a program offers has to
cause things to happen when a mouse-click or keyboard entry occurs.
These things become possible when your Java program can respond to
user events, which is called event handling, the activity you learn about dur-
ing this hour.
Getting Your Programs to Listen
Auser eventin Java issomething that happens when a user performs an
action with the mouse, keyboard, or another input device.
Before you can receive events, you must learn how to make an object lis-
ten. Respondingto user events requires the use of one or more
EventListenerinterfaces. Interfacesare a feature of object-oriented pro-
gramming in Java that enable a class to inherit behavior it would not be
able to employ otherwise. They’re like a contract agreement with other
classes that guarantee the class will contain specific methods.
An EventListenerinterface contains methods that receive user input of a
specific type.
Adding an EventListenerinterface requires two things. First, because the
listening classes are part of the java.awt.eventpackage, you must make
them available with the following statement:
importjava.awt.event.*;
HOUR 15
Responding to User Input