Java Reference
In-Depth Information
Figure 7.3 The
CaptureEvtApplet
program illustrates mouse
event handling. Whenever
the mouse moves the
cursor into the top area or
clicks while the cursor is
over the top panel, a
message will be displayed
in the lower text area.
can be generated (see Figure 7.3). The CaptureEventPanel implements the
MouseListener interface and so must provide all five methods.
Note that in the saySomething() method, the invocation of getClass().
getName() provides the name of any class as a string. Here we have used this
technique to find the identity of the component that generated the event. (See the
We b Course Chapter 5: Supplements section for more about the Class class and
the information it provides about a class definition.)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/** Demonstrate a MouseListener component. **/
public class CaptureEvtApplet extends JApplet
{
/** Create the interface with CaptureEventPanel. **/
public void init () {
Container content - pane = getContentPane ();
// Create an instance of the JPanel subclass
CaptureEventPanel cap - evt - panel =
new CaptureEventPanel ();
// And the panel to the JApplet panel.
content - pane.add (cap - evt - panel);
} // init
} // class CaptureEvtApplet
Search WWH ::




Custom Search