Suppose you want to listen to a mouse operation, assuming that the mouse is pressed, released, and so on. You can use the MouseListener interface.
Package Com.beyole.util;import Java.awt.event.mouseevent;import Java.awt.event.mouselistener;import Java.awt.event.windowadapter;import Java.awt.event.windowevent;import Javax.swing.jframe;import Javax.swing.jscrollpane;import Javax.swing.jtextarea;class Mymousehandle extends JFrame implements MouseListener { Private JTextArea Text = new JTextArea ();p ublic mymousehandle () {super.settitle ("Crystal");//Set caption JScrollPane pane = new J ScrollPane (text);//Add scroll bar pane.setbounds (5, 5, 300, 200);//Set Absolute position super.add (pane);//Add Component Text.addmouselistener to window ( this);//Increase mouse monitor super.setsize (310, Super.addwindowlistener); super.setvisible (true); public void windowclosing (WindowEvent arg0) {system.exit (1);}}); public void mouseclicked (MouseEvent e)//mouse click event {int c = E.getbutton ();//Get pressed mouse button string mouseinfo = null;//Receive information if (c = = M Ouseevent.button1)//inference is left mouse button pressed {mouseinfo = "left key";} else if (c = = Mouseevent.button3) {//Inference is right mouse button pressed Mouseinfo = "right button";} else { Mouseinfo = "Roller";} Text.append ("Mouse click: "+ Mouseinfo +". \ n "); public void mouseentered (MouseEvent e)//mouse enters component {text.append ("mouse enters component. \ n");} public void mouseexited (MouseEvent e)//mouse exit Component {Text.append ("mouse exit component. \ n");} public void mousepressed (MouseEvent e)//Mouse Press {text.append ("mouse down. \ n"); public void mousereleased (MouseEvent e)//mouse release {text.append ("mouse release. \ n");} public class Mymouseeventdemo {public static void main (string[] args) {new Mymousehandle ();}}
Program:
Java Swing Interface Programming (+)---event handling: mouse events and listening processing