home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch08 / MouseLocation.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  1.2 KB  |  53 lines

  1. import javax.swing.*;
  2. import java.awt.event.*;
  3.  
  4. import java.awt.*;
  5.  
  6. public class MouseLocation extends JPanel
  7. {
  8.     public MouseLocation()
  9.     {
  10.        setToolTipText("xxx");
  11.        setBackground(Color.white);
  12.     }//constructor
  13.  
  14.     public String getToolTipText(MouseEvent event)
  15.     {
  16.  
  17.         int x = event.getX();
  18.         int y = event.getY();
  19.         String str = "(" + x + "," + y + ")";
  20.  
  21.         return str;
  22.     }//getToolTipText
  23.  
  24.  
  25.     public static void main(String s[])
  26.     {
  27.         JFrame f = new JFrame("MouseLocation Example");
  28.         MouseLocation panel = new MouseLocation();
  29.  
  30.         f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  31.         f.setForeground(Color.black);
  32.         f.setBackground(Color.lightGray);
  33.         f.getContentPane().add(panel,"Center");
  34.         f.setSize(300,300);
  35.         f.setVisible(true);
  36.         f.addWindowListener(new WindowCloser());
  37.     }//main
  38.  
  39. }//class MouseLocation
  40.  
  41. class WindowCloser extends WindowAdapter
  42. {
  43.     public void windowClosing(WindowEvent e)
  44.     {
  45.         Window win = e.getWindow();
  46.         win.setVisible(false);
  47.         win.dispose();
  48.         System.exit(0);
  49.     }//windowClosing
  50. }//class WindowCloser
  51.  
  52.  
  53.