Source of MouseDetailsFrame.java


  1: // Fig. 11.31: MouseDetailsFrame.java
  2: // Demonstrating mouse clicks and distinguishing between mouse buttons.
  3: import java.awt.BorderLayout;
  4: import java.awt.Graphics;
  5: import java.awt.event.MouseAdapter;
  6: import java.awt.event.MouseEvent;
  7: import javax.swing.JFrame;
  8: import javax.swing.JLabel;
  9: 
 10: public class MouseDetailsFrame extends JFrame 
 11: {
 12:    private String details; // String representing 
 13:    private JLabel statusBar; // JLabel that appears at bottom of window
 14: 
 15:    // constructor sets title bar String and register mouse listener
 16:    public MouseDetailsFrame()
 17:    {
 18:       super( "Mouse clicks and buttons" );
 19: 
 20:       statusBar = new JLabel( "Click the mouse" );
 21:       add( statusBar, BorderLayout.SOUTH );
 22:       addMouseListener( new MouseClickHandler() ); // add handler
 23:    } // end MouseDetailsFrame constructor
 24: 
 25:    // inner class to handle mouse events
 26:    private class MouseClickHandler extends MouseAdapter 
 27:    {
 28:       // handle mouse click event and determine which button was pressed
 29:       public void mouseClicked( MouseEvent event )
 30:       {
 31:          int xPos = event.getX(); // get x position of mouse
 32:          int yPos = event.getY(); // get y position of mouse
 33: 
 34:          details = String.format( "Clicked %d time(s)", 
 35:             event.getClickCount() );
 36:       
 37:          if ( event.isMetaDown() ) // right mouse button   
 38:             details += " with right mouse button";
 39:          else if ( event.isAltDown() ) // middle mouse button
 40:             details += " with center mouse button";
 41:          else // left mouse button                       
 42:             details += " with left mouse button";
 43: 
 44:          statusBar.setText( details ); // display message in statusBar
 45:       } // end method mouseClicked
 46:    } // end private inner class MouseClickHandler
 47: } // end class MouseDetailsFrame
 48: 
 49: 
 50: /**************************************************************************
 51:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 52:  * Pearson Education, Inc. All Rights Reserved.                           *
 53:  *                                                                        *
 54:  * DISCLAIMER: The authors and publisher of this book have used their     *
 55:  * best efforts in preparing the book. These efforts include the          *
 56:  * development, research, and testing of the theories and programs        *
 57:  * to determine their effectiveness. The authors and publisher make       *
 58:  * no warranty of any kind, expressed or implied, with regard to these    *
 59:  * programs or to the documentation contained in these books. The authors *
 60:  * and publisher shall not be liable in any event for incidental or       *
 61:  * consequential damages in connection with, or arising out of, the       *
 62:  * furnishing, performance, or use of these programs.                     *
 63:  *************************************************************************/