Mouse Event
Mouse Event
Mouse Event
6.
/* Mouse Event Handling in a Frame Window Example This java example shows how to handle mouse events in a Frame window using MouseListener. */ java.awt.Frame; java.awt.Graphics; java.awt.event.MouseEvent; java.awt.event.MouseListener; java.awt.event.WindowAdapter; java.awt.event.WindowEvent;
a stand alone window, class should be extended from not from Applet class.
19. public
20.
class HandleMouseListenerInWindowExample extends Frame implements MouseListener{ int x=0, y=0; String strEvent = ""; HandleMouseListenerInWindowExample(String title){ //call superclass constructor with window title super(title); //add window listener addWindowListener(new MyWindowAdapter(this)); //add mouse listener addMouseListener(this); //set window size setSize(300,300); //show the window setVisible(true); }
21. 22.
23.
24.
25.
26. 27.
28.
29. 30.
31.
32. 33.
34.
35. 36.
37.
44.
45.
49. 50.
51. 52.
repaint(); }
59.
60. 61.
68.
69. 70.
77.
78. 79.
86.
87. 88.
public void paint(Graphics g){ g.drawString(strEvent + " at " + x + "," + y, 50,50); } public static void main(String[] args) { HandleMouseListenerInWindowExample myWindow = new HandleMouseListenerInWindowExample("Window With Mouse Events Example"); }
93.
94.
99.
100.
101.
} class MyWindowAdapter extends WindowAdapter{ HandleMouseListenerInWindowExample myWindow = null; MyWindowAdapter(HandleMouseListenerInWindowExample this.myWindow = myWindow; } public void windowClosing(WindowEvent we){ myWindow.setVisible(false); } }
102.
103.
104.
105.
106.
myWindow){
107. 108.
109.
xample Output