Java Reference
In-Depth Information
mouse entered event when the mouse pointer
passes into its graphical space. Likewise, it generates a
A component will generate a
mouse exited event when
the mouse pointer leaves.
Mouse motion events, as the name implies, occur while
the mouse is in motion. The mouse moved event indicates
simply that the mouse is in motion. The mouse dragged
event is generated when the user has pressed the mouse but-
ton down and moved the mouse without releasing the but-
ton. Mouse motion events are generated many times, very
quickly, while the mouse is in motion.
In a specific situation, we may care about only one or two mouse events. What
we listen for depends on what we are trying to accomplish.
The Dots program shown in Listing 8.17 responds to one mouse event.
Specifically, it draws a green dot at the location of the mouse pointer whenever
the mouse button is pressed.
The main method of the Dots class creates a frame and adds one panel to it.
That panel is defined by the DotsPanel class shown in Listing 8.18.
KEY CONCEPT
Moving the mouse and clicking the
mouse button generate events to
which a program can respond.
LISTING 8.17
//********************************************************************
// Dots.java Author: Lewis/Loftus
//
// Demonstrates mouse events.
//********************************************************************
import javax.swing.JFrame;
public class Dots
{
//-----------------------------------------------------------------
// Creates and displays the application frame.
//-----------------------------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Dots");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add ( new DotsPanel());
frame.pack();
frame.setVisible( true );
}
}
 
Search WWH ::




Custom Search