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.