Java Reference
In-Depth Information
from this that the methods in the other listener interfaces correspond to event IDs for the other event classes,
well, you're right. All the IDs for mouse events are defined in the MouseEvent class. These are:
MOUSE_CLICKEDMOUSE_PRESSEDMOUSE_DRAGGED
MOUSE_ENTEREDMOUSE_EXITED MOUSE_RELEASED
MOUSE_MOVED MOUSE_WHEEL
The MOUSE_MOVED event corresponds to just moving the mouse. The MOUSE_DRAGGED event arises when
you move the mouse while keeping a button pressed.
The event IDs that the KeyEvent class defines are:
KEY_TYPEDKEY_PRESSEDKEY_RELEASED
Those defined in the FocusEvent class are:
FOCUS_GAINEDFOCUS_LOST
To implement a listener for a particular event type, you just need to implement the methods declared in
the corresponding interface. You could handle some of the window events for the SketcherFrame window
by making the application class the listener for window events. You must then create a Sketcher object that
is the listener for the events.
TRY IT OUT: Implementing a Low-Level Event Listener
First, delete the call to the enableEvents() method in the SketcherFrame() constructor. Then delete
the definition of the processWindowEvent() method. Now you can modify the Sketcher class in the
Sketcher application so that it is a listener for window events:
// Sketching application
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Sketcher implements WindowListener {
public static void main(String[] args) {
theApp = new Sketcher();
// Create the
application object
SwingUtilities.invokeLater(
new Runnable() { //
Anonymous Runnable class object
public void run() { // Run
method executed in thread
theApp.createGUI();
// Call GUI
creator
}
});
}
// Method to create the application GUI
Search WWH ::




Custom Search