在 Java 中,按下鼠标按钮时*连续* 触发什么事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1979538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Java, what event is *continuously* fired on mouse button down?
提问by Jean-Yves
I would like to know if there exists in Java an event that is continuouslyfired when a mouse button is being held down (pressed), even if the mouse is not moved. I could not find it in typical MouseListeners:
我想知道 Java 中是否存在在按住(按下)鼠标按钮时连续触发的事件,即使鼠标没有移动。我在典型的 MouseListeners 中找不到它:
- MouseDragged is fired only if the user movethe mouse while holding the button down
- MousePressed is fired only once when the button has beenpressed
- 只有当用户在按住按钮的同时移动鼠标时才会触发 MouseDragged
- MousePressed 仅在按下按钮时触发一次
And that's about it. Any idea how to make such an event?
就是这样。知道如何举办这样的活动吗?
Cheers
干杯
jy
jy
采纳答案by ZoFreX
James Goodwin's code will not work. mousePressed and mouseReleased are both fired from the GUI thread, so blocking in mousePressed will prevent mouseReleased from ever being fired, meaning the loop will continue forever.
James Goodwin 的代码将不起作用。mousePressed 和 mouseReleased 都是从 GUI 线程触发的,所以在 mousePressed 中阻塞将阻止 mouseReleased 被触发,这意味着循环将永远持续下去。
If you already have a seperate thread for processing then use mousePressed to indicate to that thread that the event should start, and mouseReleased to stop.
如果您已经有一个单独的线程进行处理,则使用 mousePressed 向该线程指示该事件应该开始,并使用 mouseReleased 停止。
If you don't have a separate thread and don't want the hassle, a Timer is probably the easiest way. javadoc on Timer.
如果您没有单独的线程并且不想麻烦,那么计时器可能是最简单的方法。计时器上的 javadoc。
Specifically, you should create a TimerTask that does whatever it is you want to do multiple times and queue it using Timer.schedule:
具体来说,您应该创建一个 TimerTask,它可以多次执行您想做的任何事情,并使用 Timer.schedule 将其排队:
Timer timer = new Timer();
TimerTask task = new MyTimerTask();
private class MyTimerTask extends TimerTask {
public void run() {
// your code here
}
}
public void mousePressed(MouseEvent e) {
timer.scheduleAtFixedRate(task, 0, 1000); // Time is in milliseconds
// The second parameter is delay before the first run
// The third is how often to run it
}
public void mouseReleased(MouseEvent e) {
task.cancel();
// Will not stop execution of task.run() if it is midway
// But will guarantee that after this call it runs no more than one more time
}
I'm pretty sure this is the simplest way, as it doesn't involve faffing around with inter-thread communication.
我很确定这是最简单的方法,因为它不涉及线程间通信。
Oh, and as Peter said, you will have to add code to account for the user mousing down on your app and mousing up somewhere else.
哦,正如 Peter 所说,您将不得不添加代码来解释用户在您的应用程序上向下移动并在其他地方向上移动的情况。
回答by Frank
There is an obvious reason why this event is not available in MouseListener: it's going to spam you with events such that everything is slowed down to a halt. Do you want to receive this event every second, every ms, or even more often? If you need that you'll have to do it yourself.
此事件在 MouseListener 中不可用有一个明显的原因:它会向您发送垃圾邮件,从而使一切都变慢以至停止。您是否希望每秒、每毫秒甚至更频繁地接收此事件?如果你需要,你必须自己做。
For stearing this process you of course need mousePressedand mouseReleasedto determine whether a button is currently held down. Then you need to run some kind of loop that generates the corresponding events you'd like to have.
为了引导这个过程,你当然需要mousePressed并mouseReleased确定一个按钮当前是否被按下。然后您需要运行某种循环来生成您想要的相应事件。
You might also want to work via polling, i.e. extend your MouseListener class such that it can tell you whether a button is still held down, and whereever you need those events you can actively poll for the button. It depends on how you want to use these events, which approach is more suitable.
您可能还想通过轮询来工作,即扩展您的 MouseListener 类,以便它可以告诉您按钮是否仍然被按住,并且无论您在何处需要这些事件,您都可以主动轮询按钮。这取决于您想如何使用这些事件,哪种方法更合适。
回答by James Goodwin
If you need to do something whilst the mouse button is down, just start it when you detect a mousePressedevent and then continuously do that until you detect a mouseReleasedevent. Then you don't need to have your event continuously firing. e.g.
如果您需要在按下鼠标按钮时执行某些操作,只需在检测到mousePressed事件时启动它,然后继续执行此操作直到检测到mouseReleased事件。那么你不需要让你的事件持续触发。例如
public void mousePressed(MouseEvent e) {
someCondition = true;
while(someCondition) {
//do something
}
}
public void mouseReleased(MouseEvent e) {
someCondition = false;
}
EDIT:
As others have said, the code would need to be separated from the event thread otherwise the call to mouseReleasedwould get blocked preventing the loop from ending.
编辑:正如其他人所说,代码需要与事件线程分开,否则调用mouseReleased将被阻塞,阻止循环结束。
回答by Peter
There is no such event
没有这样的事件
You can create your own by starting a timer in the mousedown method and ending the same timer in the mousereleased
您可以通过在 mousedown 方法中启动计时器并在 mousereleased 中结束相同的计时器来创建自己的计时器
You will also need a few fail saves to make sure the timer stops when you do mousedown the movemove onto another component or even onto other frame or non java gui parts
您还需要进行一些失败保存,以确保当您将鼠标移到另一个组件甚至其他框架或非 Java gui 部件时,计时器停止
回答by Dave Kirby
I think you'll find the answer is no, there is no such event. Not just for Java, but for any GUI framework. It would serve no purpose other than to tie up the event queue.
我想你会发现答案是否定的,没有这样的事件。不仅适用于 Java,也适用于任何 GUI 框架。除了占用事件队列之外,它没有任何用处。
You would need to create your own by trapping the mouse down event then having a timer fire events at regular intervals until the mouse up event.
您需要通过捕获鼠标按下事件然后让计时器定期触发事件来创建自己的事件,直到鼠标按下事件。

