swing--Mouse (Action) __java Basics

Source: Internet
Author: User
Tags gety

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

After reading this article, you may learn the following knowledge:

(1) javaswing in the mouse to monitor common listeners, including MouseListener, Mousewheellistener, Mousemotionlistener.

(2) different to the mouse listener to the effect of what each is, the mouse to do what operation before calling.

(3) The difference between MouseListener and Mouseadapter, it is best to use mouseadapter rather than MouseListener, which will make the code more tidy.

(4) Further understanding of these listening through examples.


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

A: When the mouse is pressed for the first time, call mousepressed, the mouse is released, call Mousereleased, 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: You need to use the GetClickCount method if you want to distinguish between clicking, double-clicking, and three-click.

It is to be noted that:

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

Outside the component Mousedragged method will also be called

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

(3) Most of the applications are interested in mouse clicks, and the mouse movement is not interested, but the mouse movement occurred to the frequency is very high, so the mouse movement events and drag events are defined in a separate

Mousemotionlistener This is 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 you do not need to implement all MouseListener methods, you can use the Mouseadapter to select the need to make

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

(3) Mousemotionadapter also expands the Mousemotionlistener.

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


The/**mouselistener interface contains 5 mouse methods **/

Button.addmouselistener (New MouseListener () {
	@Override public
	void mouseclicked (MouseEvent e) {
		/** Called when the mouse button is clicked (pressed and released) on the component. **/
	}
	is called @Override public void mouseentered (MouseEvent e) {
		/** mouse enters the component. **/
	}
	is invoked @Override public void mouseexited (MouseEvent e) {
		/** mouse leaves the component. **/
	}

	@Override public
	void mousepressed (MouseEvent e) {
		/** mouse button is invoked when it is pressed on the component.
	called **/}

	@Override public
	void mousereleased (MouseEvent e) {
		/** mouse button is released on the component. **/
	}}
);


/** abstract class Mouseadapter (rewrite only the required methods), you are free to decide which methods you need to listen to **/

Button.addmouselistener (New Mouseadapter () {
	/** here Select Mouseclicked Method **/
	@Override public
	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 () {
	@Override public
	void mousedragged (MouseEvent e) {
		The/** mouse button is invoked when you press and drag on the component. **/
	}

	@Override public
	void mousemoved (MouseEvent e) {
		/** mouse cursor is moved to the component but no key is pressed. **/
	}
});


/** the Listener interface 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 () {
	@Override public
	void Mousewheelmoved ( Mousewheelevent e) {
		/** called when the mouse wheel rotates. **/
	}
});

The mouse listener instance looks like this:

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 Monitor * Create, erase, and move squares on the created canvas * 1. Click the mouse to create a square * 2. In the existing square double click will erase the square * 3. Use the mouse to drag the box can be freely dragged * * public class Testmouselistene R {public static void main (string[] args) {Eventqueue.invokelater (new Runnable () {@Override public void run ()
				{Mouseframe frame = new Mouseframe ();
				Frame.setdefaultcloseoperation (Jframe.exit_on_close);
			Frame.setvisible (TRUE);
	}
		});
		Class Mouseframe extends JFrame {public mouseframe () {settitle ("Mouse Listener 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);
		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 ();
		Double 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) = NULL;
		Square.remove (s);
	Repaint (); Private class Mousehandler extends Mouseadapter {@Override public void mousepressed (MouseEvent e) {current = Find (E.getpoint ());
		if (current = = null) Add (E.getpoint ());
			@Override public void mouseclicked (MouseEvent e) {current = Find (E.getpoint ());
		if (current!= null && e.getclickcount () >= 2) remove (current);  The private class Mousemotionhandler implements Mousemotionlistener {@Override public void mousemoved (mouseevent
			e) {if (Find (E.getpoint ()) = = null) setcursor (Cursor.getdefaultcursor ());
		else SetCursor (Cursor.getpredefinedcursor (cursor.crosshair_cursor));
				@Override public 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 ();
 }
		}
	}
}



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.