Java Reference
In-Depth Information
Event-adapter class in java.awt.event
Implements interface
ComponentAdapter
ComponentListener
ContainerAdapter
ContainerListener
FocusAdapter
FocusListener
KeyAdapter
KeyListener
MouseAdapter
MouseListener
MouseMotionAdapter
MouseMotionListener
WindowAdapter
WindowListener
Fig. 12.30 | Event-adapter classes and the interfaces they implement.
Extending MouseAdapter
The application of Figs. 12.31-12.32 demonstrates how to determine the number of
mouse clicks (i.e., the click count) and how to distinguish between the different mouse
buttons. The event listener in this application is an object of inner class MouseClickHan-
dler (Fig. 12.31, lines 25-46) that extends MouseAdapter , so we can declare just the
mouseClicked method we need in this example.
1
// Fig. 12.31: MouseDetailsFrame.java
2
// Demonstrating mouse clicks and distinguishing between mouse buttons.
3
import java.awt.BorderLayout;
4
import java.awt.event.MouseAdapter;
5
import java.awt.event.MouseEvent;
6
import javax.swing.JFrame;
7
import javax.swing.JLabel;
8
9
public class MouseDetailsFrame extends JFrame
10
{
11
private String details; // String displayed in the statusBar
12
private final JLabel statusBar; // JLabel at bottom of window
13
14
// constructor sets title bar String and register mouse listener
15
public MouseDetailsFrame()
16
{
17
super ( "Mouse clicks and buttons" );
18
19
statusBar = new JLabel( "Click the mouse" );
20
add(statusBar, BorderLayout.SOUTH );
21
addMouseListener( new MouseClickHandler()); // add handler
22
}
23
24
// inner class to handle mouse events
25
private class MouseClickHandler extends MouseAdapter
26
{
27
// handle mouse-click event and determine which button was pressed
28
@Override
29
public void mouseClicked(MouseEvent event)
30
{
Fig. 12.31 | Demonstrating mouse clicks and distinguishing between mouse buttons. (Part 1 of 2.)
 
Search WWH ::




Custom Search