MouseListener 触发速度不够快

发布于 2024-09-17 19:22:18 字数 316 浏览 11 评论 0原文

我有一个扩展 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神爱温柔 2024-09-24 19:22:18

尝试使用 mousePressed 而不是 mouseClicked。 mouseClicked 查找多个按钮单击,因此它将合并一些事件。

Try using mousePressed instead of mouseClicked. mouseClicked looks for multiple button clicks, so it will coalesce some events.

喜爱纠缠 2024-09-24 19:22:18

希望这对 3.5 年后寻找同一问题答案的人有所帮助:)

当您单击鼠标时,您将触发以下事件。

  1. MousePressed
  2. MouseDragged(如果您按得足够用力来移动
    光标轻微)
  3. MouseReleased
  4. MouseClicked

我遇到了这个问题,在 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.

  1. MousePressed
  2. MouseDragged (if you pressed hard enough to move the
    cursor slightly)
  3. MouseReleased
  4. MouseClicked

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.

自我难过 2024-09-24 19:22:18

扩展一下 @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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文