Java MouseEvent,检查是否按下 [英] Java MouseEvent, check if pressed down
问题描述
我有一个实现MouseListener(JPanel)的类.当我在面板上单击时,会发生某些事情.我想要的是某种while循环,只要按下鼠标左键即可循环.
I have a class that implements MouseListener (JPanel). When I click on the panel something happens. What I want is some kind of while-loop that loops as long as left mousebutton is pressed down.
@Override
public void mousePressed(MouseEvent e) {
while(e.isPressedDownD) { // <--
//DO SOMETHING
}
}
这显然行不通,但我希望您了解我正在努力实现的目标. 全班学习:
This obviously doesn't work, but I hope you understand what I'm trying to achieve. The whole class for those that are interested:
package control;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import model.GridModel;
import view.GUIView;
public class MapListener implements MouseListener{
private GridModel model;
private GUIView view;
private int posX;
private int posY;
public MapListener(GridModel model, GUIView view) {
this.model = model;
this.view = view;
}
@Override
public void mouseClicked(MouseEvent e) {
posX = e.getX();
posY = e.getY();
model.setMouseAtX(posX);
model.setMouseAtY(posY);
view.paintTile();
System.out.println("X: " + posX + " Y: " + posY);
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent e) {
while(e.getModifiers() == MouseEvent.MOUSE_PRESSED) { //Obviously doesn't work
//DO SOMETHING
}
}
@Override
public void mouseReleased(MouseEvent arg0) {
}
}
推荐答案
其他答案指出,在鼠标事件侦听器方法中,不是是完成工作的地方.
As pointed out by other answers, the place to do your work is not in the mouse event listener methods.
在MouseEvent
中也没有明确的按鼠标"概念,因此您必须自己进行跟踪.我提供了如何执行此操作的示例.还要注意MouseEvent.BUTTON1
引用,因为这只是为了跟踪鼠标左键的状态.
Also there is no explicit "mouse pressed" notion in MouseEvent
, so you must track that yourself. I have provided an example of how to do this. Also note the MouseEvent.BUTTON1
references, as this is just to track the state of the left mouse button.
这是您必须开始学习并发性的地方.因此,我添加了一个同步方法,因为您需要注意,当多个线程同时访问属性时会发生有趣的事情,而synchronized
是保持这种理智的机制.在本示例的范围之外,请考虑进一步阅读.
This is where you must start to learn about concurrency. For that reason, I've added in a synchronized method as you need to be aware that funny things happen when multiple threads access properties at the same time, and synchronized
is a mechanism for keeping this sane. Consider it further reading beyond the scope of this example.
未经测试,但这应该可以工作:
Untested, but this should work:
volatile private boolean mouseDown = false;
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
mouseDown = true;
initThread();
}
}
public void mouseReleased(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
mouseDown = false;
}
}
volatile private boolean isRunning = false;
private synchronized boolean checkAndMark() {
if (isRunning) return false;
isRunning = true;
return true;
}
private void initThread() {
if (checkAndMark()) {
new Thread() {
public void run() {
do {
//do something
} while (mouseDown);
isRunning = false;
}
}.start();
}
}
这篇关于Java MouseEvent,检查是否按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!