Java Mouse Events

Source: Internet
Author: User

What is the difference between the mouseclicked () method and the Mousepressed () method in the MouseListener method interface

This is a hot topic, let's go deep to find the answer, the Java ' AWT Library provides two interfaces to listen and receive mouse events. One is Java.awt.event.MouseListener:
Public interface MouseListener extends EventListener
{
public void mousepressed (MouseEvent e);
public void mousereleased (MouseEvent e);
public void mouseclicked (MouseEvent e);
public void mouseentered (MouseEvent e);
public void mouseexited (MouseEvent e);
}

The other is Java.awt.event.MouseMotionListener:

Public interface Mousemotionlistener extends EventListener
{
public void mousedragged (MouseEvent e);
public void mousemoved (MouseEvent e);
}

Swing provides Mouseinputlistener, which extends both Mousemotionlistener and MouseListener interfaces.

Let's take a look at the methods in these interfaces:

Mousepressed () Occurs when the user presses the mouse button.

Mousereleased () Occurs when the user releases the mouse button.

Mouseclicked () Occurs when the user presses and releases the mouse button. The user usually clicks the mouse button when selecting or double-clicking the icon. If the user moves the mouse before releasing the mouse, clicking does not cause the mouse to appear accordingly.

The mousepressed () and mousereleased () methods have been invoked at the same time before the event is assigned to the Mouseclicked () method, because the mouse click is a combination of pressing the mouse and releasing the mouse.

Mouseentered () Activates the event when the mouse leaves the current component and enters the component you are listening to.

Mouseexited () Occurs when the mouse leaves the component that you are listening to.

Mousedragged () Occurs when the user presses the mouse button and moves before it is released. Releasing the mouse after mousedragged () does not cause mouseclicked ().

Mousemoved () Occurs when the mouse moves on a component and drags it from time to moment.

To listen for mouse events, you must call one of these interfaces, or extend a mouse adapter (mouse adapters) class. AWT provides two listening adapters (Listener adapters): Java.awt.event.MouseAdapter and Java.awt.event.MouseMotionAdapter.

Swing provides Mouseinputlistener with an adapter called Javax.swing.event.MouseInputAdapter. With adapters you don't have to call every method in the interface. Instead, you can simply extend the adapter and rewrite the method you want to monitor.

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.