MouseListener 触发速度不够快
我有一个扩展 JFrame 的类,它正在监视任何地方的鼠标单击:
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
System.out.println("mouse was clicked");
}
});
我通常必须在单击之间等待近一秒钟才能触发事件。如果我在一秒钟内点击 2 或 3 次,则只会触发一个事件。如何观察快速点击事件?
这是我第一次使用 Java,我正在使用 NetBeans。
I have a Class extending JFrame that is watching for a mouse click anywhere:
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
System.out.println("mouse was clicked");
}
});
I usually have to wait nearly a second between clicks to trigger the event. If I make 2 or 3 clicks in a second, only one event fires. How do you watch for fast click events?
This is my first time using Java and I'm using NetBeans.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用 mousePressed 而不是 mouseClicked。 mouseClicked 查找多个按钮单击,因此它将合并一些事件。
Try using mousePressed instead of mouseClicked. mouseClicked looks for multiple button clicks, so it will coalesce some events.
希望这对 3.5 年后寻找同一问题答案的人有所帮助:)
当您单击鼠标时,您将触发以下事件。
光标轻微)
我遇到了这个问题,在 Netbeans 中使用其 Forms 实用程序以惰性方式处理事件。
我发现在按下和释放之间意外拖动鼠标导致了单击事件的终止。
按预期工作还是 JVM 和 Netbeans 的小故障?我不知道。
我使用的解决方法是注册 MousePressed 和 MouseReleased 事件来模拟单击。
如果按下和释放没有发生在同一个对象上,MouseReleased 将不执行任何操作。
如果按下和释放发生在同一个对象上,我会使用适当的参数调用我的方法 请注意,由于我正在处理
JFrame 上的点击,因此它是唯一的摆动对象,因此我传递了鼠标坐标的 Point 对象并比较两者,确保它们落在指定的矩形。
Hopefully this helps 3.5 years later for anyone searching for answers to the same problem :)
When you click on the mouse you will fire the following events.
cursor slightly)
I ran into this very problem making the events the lazy way in Netbeans using their Forms utility.
I found the accidental dragging of my mouse between Press and Release is what killed the click event.
Working as intended or a minor failing of the JVM and Netbeans? I don't know.
The work-around I used was to register a MousePressed and MouseReleased event to simulate the clicking.
If the Press and Release do not happen on the same Object, MouseReleased will do nothing.
If the Press and Release happen on the same Object, I call my method with appropriate parameters to consume the event.
Note that since I'm handling clicks on the JFrame, so it IS the only swing object, so I'm passing a Point object of the mouse coords and comparing both, ensuring they fall within the specified rectangle.
扩展一下 @Ricky Clarkson 所说的内容:每次按下鼠标按钮时,MousePressed 都会触发;每次释放鼠标按钮时,MouseReleased 都会触发,每次
操作系统感觉用户已完成单击时,MouseClicked 事件都会触发(即,他们单击的次数足以溢出单击计数,或者距离上次单击还有足够的时间)以便将其算作完成的单击)。用户按下并释放鼠标事件。如果您需要有关鼠标按下的信息,请使用 MousePressed 事件。否则,每当操作系统想要将 MouseClicked 事件交给 Java 时,您都会收到 MouseClicked 事件,这在很大程度上取决于系统的设置(即在系统选项中设置的延迟时间 - 如控制面板 - 以允许双击)。希望这有助于澄清。
编辑:删除了我与操作系统信息相关的陈述 - 看来我错误地回忆了它是如何工作的。我很抱歉。
To expand a little on what @Ricky Clarkson said: MousePressed will fire every time a mouse button is pressed; MouseReleased will fire every time a mouse button is released, and MouseClicked events will fire every time
the OS feels that the user is done clicking (i.e. they have clicked enough to overflow the click count or there was enough time from their last click for it to count as the finished click).the user presses and releases the mouse event.If you want information on a mouse press, then use the MousePressed event. Otherwise, you will get the event of a MouseClicked whenever the OS wants to give it to Java, which can depend greatly on the settings of the system (i.e. how long of a delay is set in the System options - like the Control Panel - to allow for double clicks).Hope this helps clarify.
Edit: Removed my statements related to the OS information - it seems I was mistaken in my recollection of how this worked. My apologies.