Java Reference
In-Depth Information
triggered by AWT components. (The java.awt.event package is not a subset of
java.awt; it is a different package.) Table 3-15 lists several classes in the
java.awt.event package, an example of an event represented by the class, and an
associated listener interface.
Table 3-15
java.awt.event Classes
ASSOCIATED LISTENER
CLASS
EXAMPLE OF EVENT
INTERFACE
ActionEvent
User clicks a button, presses the ENTER
ActionListener
key, or chooses a menu item
ItemEvent
User selects or deselects an item such as
ItemListener
a checkbox or an option button
KeyEvent
User presses a key
KeyListener
MouseEvent
User performs a mouse action, such
MouseListener
as select, drag, or enter
MouseMotionListener
TextEvent
User changes text in text box
TextListener
WindowEvent
Window changes status by opening,
WindowListener
closing, or performing some other action
As shown in Table 3-15, every event class has one or more associated listener
interfaces. A listener interface — sometimes called simply an interface
monitors, or listens, for events during execution of an interactive program. The
listener interface used in the applet version of the Body Mass Index Calculator is
ActionListener. ActionListener is a listener interface that listens for any events
that occur during execution of the program, such as when a user clicks a button,
double-clicks an item, selects a menu item, or presses the ENTER key. The
ActionListener tells the program that some response to the user's action should
occur. The keyword, implements , is used in the class header to specify which
listener interface a programmer wants to use. If more than one listener interface
is implemented, those are separated by commas.
Figure 3-37 on the next page shows the comments, import statements, class
header, and declarations for the applet. As shown in line 5, the new name of the
program will be BodyMassApplet. The applet will import three packages, extend
the Applet class, and implement the ActionListener. The BodyMassApplet will
declare the same int and double variables that were declared in the BodyMass
and BodyMassSwing programs. It also will declare an Image named logo.
 
Search WWH ::




Custom Search