Java Reference
In-Depth Information
java.lang.Object
+-java.util.EventObject
+-java.awt.AWTEvent
+-java.awt.event.ComponentEvent
+-java.awt.event.InputEvent
+-java.awt.event.MouseEvent
These include a number of methods that provide information about an event. (See
the Java 2 API Specification for a detailed listing.) A sampling of the methods
include:
getComponent ()
- identifies the
Component
object that generated the event
getX ()
,
getY ()
,
getPoint ()
- provide the coordinates of the mouse location
getClickCount ()
-gives number of times the mouse button was clicked
getModifiers ()
- indicates what modifier keys were held down during the event
Both the motion of the mouse and its buttons create events. There are two
separate types of mouse event listeners:
MouseMotionListener
Each move of the mouse generates a motion event. To listen for mouse motion events,
a class needs to implement the
MouseMotionListener
interface. The implementing
class will need to provide two methods:
mouseMoved (MouseEvent e)
- mouse motion
mouseDragged (MouseEvent e)
- mouse motion when mouse button is held
down
MouseListener
The
MouseListener
interface provides these methods for mouse button and cursor
events:
mousePressed (MouseEvent e)
- mouse button is pressed
mouseReleased (MouseEvent e)
- mouse button is released
mouseClicked (MouseEvent e)
-button clicked (press and release counts as
one action)
mouseEntered (MouseEvent e)
- the cursor enters the area of the component
mouseExited (MouseEvent e)
- the cursor exits the area of the component
The following example program
CaptureEvtApplet
illustrates how to use the
MouseListener
interface to monitor when a mouse cursor enters or exits the
area of the panel and when clicks are made over the panel. The program displays
messages in a
JTextArea
to indicate the different ways that mouse events
Search WWH ::
Custom Search