Java Reference
In-Depth Information
89
90
// handle event when user moves mouse
91
@Override
92
public void mouseMoved(MouseEvent event)
93
{
94
statusBar.setText(String.format( "Moved at [%d, %d]" ,
95
event.getX() event.getY()
,
));
96
}
97
} // end inner class MouseHandler
98
} // end class MouseTrackerFrame
Fig. 12.28 | Mouse event handling. (Part 3 of 3.)
1
// Fig. 12.29: MouseTrackerFrame.java
2
// Testing MouseTrackerFrame.
3
import javax.swing.JFrame;
4
5
public class MouseTracker
6
{
7
public static void main(String[] args)
8
{
9
MouseTrackerFrame mouseTrackerFrame = new MouseTrackerFrame();
10
mouseTrackerFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
11
mouseTrackerFrame.setSize( 300 , 100 );
12
mouseTrackerFrame.setVisible( true );
13
}
14
} // end class MouseTracker
Fig. 12.29 | Testing MouseTrackerFrame .
Line 23 creates JPanel mousePanel . This JPanel 's mouse events aretracked by the app.
Line 24 sets mousePanel 's background color to white. When the user moves the mouse into
the mousePanel , the application will change mousePanel 's background color to green. When
the user moves the mouse out of the mousePanel , the application will change the background
color back to white. Line 25 attaches mousePanel to the JFrame . As you've learned, you typ-
ically must specify the layout of the GUI components in a JFrame . In that section, we intro-
Search WWH ::




Custom Search