Mouse Event Handling in a Frame Window Example

  1. /*
  2. Mouse Event Handling in a Frame Window Example
  3. This java example shows how to handle mouse events in a Frame window
  4. using MouseListener.
  5. */
  6.  
  7. import java.awt.Frame;
  8. import java.awt.Graphics;
  9. import java.awt.event.MouseEvent;
  10. import java.awt.event.MouseListener;
  11. import java.awt.event.WindowAdapter;
  12. import java.awt.event.WindowEvent;
  13.  
  14. /*
  15. * To create a stand alone window, class should be extended from
  16. * Frame and not from Applet class.
  17. */
  18.  
  19. public class HandleMouseListenerInWindowExample extends Frame implements MouseListener{
  20.  
  21. int x=0, y=0;
  22. String strEvent = "";
  23.  
  24. HandleMouseListenerInWindowExample(String title){
  25.  
  26. //call superclass constructor with window title
  27. super(title);
  28.  
  29. //add window listener
  30. addWindowListener(new MyWindowAdapter(this));
  31.  
  32. //add mouse listener
  33. addMouseListener(this);
  34.  
  35. //set window size
  36. setSize(300,300);
  37.  
  38. //show the window
  39. setVisible(true);
  40. }
  41.  
  42.  
  43.  
  44. public void mouseClicked(MouseEvent e) {
  45.  
  46. strEvent = "MouseClicked";
  47. x = e.getX();
  48. y = getY();
  49. repaint();
  50. }
  51.  
  52.  
  53. public void mousePressed(MouseEvent e) {
  54. strEvent = "MousePressed";
  55. x = e.getX();
  56. y = getY();
  57. repaint();
  58.  
  59. }
  60.  
  61.  
  62. public void mouseReleased(MouseEvent e) {
  63. strEvent = "MouseReleased";
  64. x = e.getX();
  65. y = getY();
  66. repaint();
  67.  
  68. }
  69.  
  70.  
  71. public void mouseEntered(MouseEvent e) {
  72. strEvent = "MouseEntered";
  73. x = e.getX();
  74. y = getY();
  75. repaint();
  76.  
  77. }
  78.  
  79.  
  80. public void mouseExited(MouseEvent e) {
  81. strEvent = "MouseExited";
  82. x = e.getX();
  83. y = getY();
  84. repaint();
  85.  
  86. }
  87.  
  88.  
  89. public void paint(Graphics g){
  90. g.drawString(strEvent + " at " + x + "," + y, 50,50);
  91. }
  92.  
  93. public static void main(String[] args) {
  94.  
  95. HandleMouseListenerInWindowExample myWindow =
  96. new HandleMouseListenerInWindowExample("Window With Mouse Events Example");
  97. }
  98.  
  99.  
  100. }
  101.  
  102. class MyWindowAdapter extends WindowAdapter{
  103.  
  104. HandleMouseListenerInWindowExample myWindow = null;
  105.  
  106. MyWindowAdapter(HandleMouseListenerInWindowExample myWindow){
  107. this.myWindow = myWindow;
  108. }
  109.  
  110. public void windowClosing(WindowEvent we){
  111. myWindow.setVisible(false);
  112. }
  113. }

Example Output

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!