Java mouse events ignored when mouse moves?

I recently worked on a 2d game in java, and I'm currently trying to get the event processing system to work correctly. I tried to click the mouse to start the animation, and it worked until I tried to move the mouse by clicking. Nothing is happening at the moment. I use the mouselistener and mousemotionlistener classes and the problem still persists. Here is the code from Main:

public class ML extends MouseAdapter{ public void mouseClicked(MouseEvent m){ if(m.getButton()==MouseEvent.BUTTON1) guns.playOnce(); } public void mouseReleased(MouseEvent m){ if(m.getButton()==MouseEvent.BUTTON1); } 

It calls an animator class to simultaneously play back a set of images and stop. The animator worked fine before I turned on the mouse events. I cannot understand why it will not work during the movement of the mouse if no action is indicated during this movement of the mouse. (If there is an obvious solution, I'm sorry, I started Java not so long ago.)

+8
java swing actionlistener mouseevent
source share
1 answer

In Java, a mouse click is only registered when you click and release the mouse without moving the mouse. This is very difficult for most users, so most user interface elements (such as buttons) respond to mouse clicks and dispatch events and ignore clicks.

For a button, however, the best option is to add an ActionListener to it. Then the button itself will listen for mouse events and decide when it was clicked.

+6
source share

All Articles