Java Swing Interface Programming (+)---event handling: mouse events and listening processing

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.