swing--Mouse (Action)

Source: Internet
Author: User

This article is in the study summary, welcome reprint but please specify Source:http://blog.csdn.net/pistolove/article/details/41346969

After reading this article, you will probably learn the following knowledge:

(1) javaswing to the mouse monitor commonly used listeners, including MouseListener, Mousewheellistener, Mousemotionlistener.

(2) different to the mouse listener to the role of what each, the mouse to do what action is called.

(3) The difference between MouseListener and Mouseadapter, it is best to use mouseadapter instead of MouseListener, which makes the code more tidy.

(4) To further understand these monitoring through the example.


When the user clicks the mouse, three listener methods are invoked:

A: Call Mousepressed When the mouse is pressed for the first time, call mousereleased when the mouse is released, and finally call mouseclicked.

B: Using the MouseEvent class object as a parameter, call the Getx () and Gety () methods to get the mouse pointer to the x and Y coordinates when the mouse is pressed.

C: If you want to distinguish between clicking, double clicking, and triple clicking, you need to use the GetClickCount method.

It is important to note that:

(1) If you press the mouse while moving the mouse, you will call mousemoved instead of mousedragged; the Mousemoved method is called only if the mouse is stuck inside a component, if the mouse is dragged

The Mousedragged method is also called outside the component

(2) The mouseentered and mouseexited methods are called when the mouse enters or moves out of the component.

(3) Most of the applications are interested in mouse click, but the mouse movement is not interested, but the mouse movement events occur to a high frequency, so the mouse movement events and drag events are defined separately in the

Mousemotionlistener this independent into the interface.


MouseListener vs Mouseadapter vs Mousewheellistener vs Mousemotionlistener vs Mousemotionadapter

(1) in the above several kinds of monitoring, the comparison commonly used is mouselistener and Mousemotionlistener, but Mousewheellistener uses relatively few.

(2) Mouseadapter extended Mouselistener,mouseadapter targeted relatively strong, when it is not necessary to implement MouseListener all methods, you can use the Mouseadapter to select the need to make

Which one or several kinds of monitoring to implement, so that the code looks more concise, but also reduce the amount of code.

(3) Mousemotionadapter also expanded the Mousemotionlistener.

(4) Mousewheellistener is the mouse wheel listener, generally do not use the listener.


The/**mouselistener interface consists of 5 mouse methods **/

Button.addmouselistener (New MouseListener () {@Overridepublic void mouseclicked (MouseEvent e) {/** Called when the mouse button is clicked (pressed and released) on the component. **/} @Overridepublic void mouseentered (MouseEvent e) {/** is called when the mouse enters the component. **/} @Overridepublic void mouseexited (MouseEvent e) {/** is called when the mouse leaves the component. **/} @Overridepublic void mousepressed (MouseEvent e) {/** mouse button is called when pressed on the component. **/} @Overridepublic void mousereleased (MouseEvent e) {/** mouse button is called when it is disposed on the component. **/}});


/** Abstract class Mouseadapter (overriding only the required methods), you are free to decide which method to listen to **/

Button.addmouselistener (New Mouseadapter () {/** Here Select mouseclicked method **/@Overridepublic void mouseclicked (MouseEvent e) {super.mouseclicked (e);}});

/** the Listener interface used to receive mouse movement events on the component. For clicks and other mouse events, use MouseListener. **/

Button.addmousemotionlistener (New Mousemotionlistener () {@Overridepublic void mousedragged (MouseEvent e) {/** Called when the mouse button is pressed and dragged on the component. **/} @Overridepublic void mousemoved (MouseEvent e) {/** The mouse cursor is moved to the component but no keystrokes are pressed. **/}});


/** The Listener interface that is used to receive mouse wheel events on the component. For clicks and other mouse events, use MouseListener. For mouse movement and drag, use Mousemotionlistener. **/

Button.addmousewheellistener (New Mousewheellistener () {@Overridepublic void mousewheelmoved (Mousewheelevent e) {/** Called when the mouse wheel rotates. **/}});

The mouse listener instance is as follows:

Import Java.awt.cursor;import java.awt.eventqueue;import Java.awt.graphics;import Java.awt.graphics2d;import Java.awt.event.mouseadapter;import Java.awt.event.mouseevent;import Java.awt.event.mousemotionlistener;import Java.awt.geom.point2d;import Java.awt.geom.rectangle2d;import Java.util.arraylist;import Java.util.List;import Javax.swing.jcomponent;import javax.swing.jframe;/** * Mouse monitoring * Create, erase, move blocks on the created canvas * 1. Clicking the mouse will create a block * 2. Double-clicking on an existing block will erase the block * 3. Using a mouse You can drag */public class Testmouselistener {public static void main (string[] args) {Eventqueue.invokelater (new Runnable () {@Overridepublic void run () {Mouseframe frame = new Mouseframe (); Frame.setdefaultcloseoperation (jframe.exit _on_close); frame.setvisible (True);}});}} Class Mouseframe extends JFrame {public mouseframe () {settitle ("Mouse Monitoring Test"); SetSize (300, 200); Mousecomponent component = new mousecomponent (); Add (component);}} Class Mousecomponent extends JComponent {private static final int sideleng = 10;private list<rectangle2d> square;priVate rectangle2d current;public mousecomponent () {square = new arraylist<rectangle2d> (); current = null; Addmouselistener (New Mousehandler ()); Addmousemotionlistener (new Mousemotionhandler ()); public void Paintcomponent (Graphics g) {graphics2d g2 = (graphics2d) g;for (rectangle2d r:square) {G2.draw (R);}} Public rectangle2d Find (point2d p) {for (rectangle2d r:square) {if (R.contains (p)) return r;} return null;}  public void Add (POINT2D p) {Double x = p.getx ();d ouble y = p.gety (); current = new Rectangle2d.double (X-SIDELENG/2, Y- Sideleng/2,sideleng, Sideleng); Square.add (current); repaint ();} public void Remove (rectangle2d s) {if (s = = null) return;if (s = = current) Current = Null;square.remove (s); repaint (); Private class Mousehandler extends Mouseadapter {@Overridepublic void mousepressed (MouseEvent e) {current = find (E.getpoi NT ()); if (current = = null) Add (E.getpoint ());} @Overridepublic void mouseclicked (MouseEvent e) {current = Find (E.getpoint ()), if (current! = null && e.getClickcount () >= 2) remove (current);}} Private class Mousemotionhandler implements Mousemotionlistener {@Overridepublic void mousemoved (MouseEvent e) {if (find (E.getpoint ()) = = null) setcursor (Cursor.getdefaultcursor ()); Elsesetcursor (Cursor.getpredefinedcursor ( cursor.crosshair_cursor));} @Overridepublic void mousedragged (MouseEvent e) {if (current! = null) {int x = E.GETX (); int y = E.gety (); Current.setframe ( X-SIDELENG/2, Y-SIDELENG/2, Sideleng,sideleng); repaint ();}}}



swing--Mouse (Action)

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.