Part 3 of 3 public void mouseEntered MouseEvent event eventgetX eventgetY

Part 3 of 3 public void mouseentered mouseevent event

This preview shows page 4 - 7 out of 16 pages.

Mouse event handling. (Part 3 of 3.) public void mouseEntered( MouseEvent event ) event.getX() event.getY() mousePanel.setBackground( Color.GREEN ); public void mouseExited( MouseEvent event ) mousePanel.setBackground( Color.WHITE ); public void mouseDragged( MouseEvent event ) event.getX() event.getY() public void mouseMoved( MouseEvent event ) event.getX() event.getY()
Image of page 4
14.14 Mouse Event Handling 593 Line 31 creates an instance of inner class MouseHandler (lines 36–90) called handler that responds to mouse events. Lines 32–33 register handler as the listener for mouse- Panel ’s mouse events. Methods addMouseListener and addMouseMotionListener are inherited indirectly from class Component and can be used to register MouseListener s and MouseMotionListener s, respectively. A MouseHandler object is a MouseListener and is a MouseMotionListener because the class implements both interfaces. We chose to imple- ment both interfaces here to demonstrate a class that implements more than one interface, but we could have implemented interface MouseInputListener instead.] When the mouse enters and exits mousePanel ’s area, methods mouseEntered (lines 62–67) and mouseExited (lines 70–74) are called, respectively. Method mouseEntered displays a message in the statusBar indicating that the mouse entered the JPanel and changes the background color to green. Method mouseExited displays a message in the statusBar indicating that the mouse is outside the JPanel (see the first sample output window) and changes the background color to white. The other five events display a string in the statusBar that includes the event and the coordinates at which it occurred. MouseEvent methods getX and getY return the x- and y - coordinates, respectively, of the mouse at the time the event occurred. 1 // Fig. 14.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 ); // set frame size 12 mouseTrackerFrame.setVisible( true ); // display frame 13 } // end main 14 } // end class MouseTracker Fig. 14.29 | Test class for MouseTrackerFrame .
Image of page 5
594 Chapter 14 GUI Components: Part 1 14.15 Adapter Classes Many event-listener interfaces, such as MouseListener and MouseMotionListener , con- tain multiple methods. It’s not always desirable to declare every method in an event-listen- er interface. For instance, an application may need only the mouseClicked handler from MouseListener or the mouseDragged handler from MouseMotionListener . Interface Win- dowListener specifies seven window event-handling methods. For many of the listener in- terfaces that have multiple methods, packages java.awt.event and javax.swing.event provide event-listener adapter classes. An adapter class implements an interface and pro- vides a default implementation (with an empty method body) of each method in the in- terface. Figure 14.30 shows several java.awt.event adapter classes and the interfaces they implement. You can extend an adapter class to inherit the default implementation of every
Image of page 6
Image of page 7

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture