Javase Mouse event class (MouseEvent) implementation

Source: Internet
Author: User

{Related Information}
A mouse event class (MouseEvent) is a mouse action event that occurs in an assembly, such as pressing the mouse, releasing the mouse, clicking the mouse, entering or leaving the component's geometry, moving the mouse, and dragging the mouse. Mouse events are triggered when the mouse is moved to an area or when the mouse clicks a component. Using mouse events, you must add an event handler for the MouseListener interface to the component that contains the following 5 methods:
void mouseclicked (MouseEvent e): Occurs when the mouse is clicked in the area
void mouseentered (MouseEvent e): Occurs when the mouse enters the area
void mouseexited (MouseEvent e): Occurs when the mouse leaves the area
void mousepressed (MouseEvent e): Occurs when the mouse is pressed in the area
void mousereleased (MouseEvent e): Occurs when the mouse is released in the area

Achieve
During the experiment, in the panel area, you can clearly see the difference between clicking and pressing. Click to release immediately is a click to get the location information.
If you press not to release, move after a while, then release, will not get the location information.

{Related Code}

 PackageSup.orange.learn;Importjava.awt.*;Importjava.awt.event.MouseEvent;ImportJava.awt.event.MouseListener;ImportJava.awt.event.WindowAdapter;Importjava.awt.event.WindowEvent;/*** Created by Re-x on 11/2/14.*/ Public classMouseeventdemoextendsFrame {panel panel;    TextField Textfield1, Textfield2;  PublicMouseeventdemo () {Super();    Init (); }     Public Static voidMain (string[] args) {NewMouseeventdemo (); }     Public voidinit () {setlayout (NewGridLayout (3, 1)); Textfield1=NewTextField (30); Textfield2=NewTextField ();        Add (textfield1);        Add (TEXTFIELD2); Panel=NewPanel ();        Panel.setbackground (Color.cyan);        Add (panel); Panel.addmouselistener (NewMouseListener () {@Override Public voidmouseclicked (MouseEvent e) {Textfield2.settext ("X=" +e.getx () + "; y=" +e.gety ()); } @Override Public voidmousepressed (MouseEvent e) {Textfield1.settext ("Mouse pressed"); } @Override Public voidmousereleased (MouseEvent e) {Textfield1.settext ("Mouse released"); } @Override Public voidmouseentered (MouseEvent e) {Textfield1.settext ("Mouse entered"); } @Override Public voidmouseexited (MouseEvent e) {Textfield1.settext ("Mouse exited");        }        }); Addwindowlistener (NewWindowadapter () {@Override Public voidwindowclosing (windowevent e) {Super. windowclosing (e);                Dispose (); System.exit (0);        }        }); SetSize (200, 300); SetVisible (true); }}

Javase Mouse event class (MouseEvent) implementation

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.