19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

618 Chapter 16 Event-Driven <strong>Programming</strong><br />

This constructs a Point object with the specified x- and y-coordinates. Normally, the data<br />

fields in a class should be private, but this class has two public data fields.<br />

<strong>Java</strong> provides two listener interfaces, MouseListener and MouseMotionListener, <strong>to</strong><br />

handle mouse events, as shown in Figure 16.13. Implement the MouseListener interface <strong>to</strong><br />

listen for such actions as pressing, releasing, entering, exiting, or clicking the mouse, and<br />

implement the MouseMotionListener interface <strong>to</strong> listen for such actions as dragging or<br />

moving the mouse.<br />

«interface»<br />

java.awt.event.MouseListener<br />

+mousePressed(e: MouseEvent): void<br />

+mouseReleased(e: MouseEvent): void<br />

+mouseClicked(e: MouseEvent): void<br />

+mouseEntered(e: MouseEvent): void<br />

+mouseExited(e: MouseEvent): void<br />

Invoked after the mouse but<strong>to</strong>n has been pressed on the source<br />

<strong>com</strong>ponent.<br />

Invoked after the mouse but<strong>to</strong>n has been released on the<br />

source <strong>com</strong>ponent.<br />

Invoked after the mouse but<strong>to</strong>n has been clicked (pressed and<br />

released) on the source <strong>com</strong>ponent.<br />

Invoked after the mouse enters the source <strong>com</strong>ponent.<br />

Invoked after the mouse exits the source <strong>com</strong>ponent.<br />

«interface»<br />

java.awt.event.MouseMotionListener<br />

+mouseDragged(e: MouseEvent): void<br />

+mouseMoved(e: MouseEvent): void<br />

Invoked after a mouse but<strong>to</strong>n is moved with a but<strong>to</strong>n pressed.<br />

Invoked after a mouse but<strong>to</strong>n is moved without a but<strong>to</strong>n pressed.<br />

FIGURE 16.13 The MouseListener interface handles mouse pressed, released, clicked, entered, and exited events. The<br />

MouseMotionListener interface handles mouse dragged and moved events.<br />

To demonstrate using mouse events, we give an example that displays a message in a panel<br />

and enables the message <strong>to</strong> be moved using a mouse. The message moves as the mouse is<br />

dragged, and it is always displayed at the mouse point. Listing 16.8 gives the program. A sample<br />

run of the program is shown in Figure 16.14.<br />

FIGURE 16.14<br />

You can move the message by dragging the mouse.<br />

VideoNote<br />

Move message using the<br />

mouse<br />

LISTING 16.8<br />

MoveMessageDemo.java<br />

1 import java.awt.*;<br />

2 import java.awt.event.*;<br />

3 import javax.swing.*;<br />

4<br />

5 public class MoveMessageDemo extends JFrame {<br />

6 public MoveMessageDemo() {

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!