当鼠标在JFrame内移动时,Swing Timer停止调用actionPerformed() [英] Swing Timer stops calling actionPerformed() while the mouse is moving inside a JFrame

查看:184
本文介绍了当鼠标在JFrame内移动时,Swing Timer停止调用actionPerformed()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将鼠标移到 javax.swing.JFrame 中, javax.swing.Timer 会停止调用 actionPerformed()方法,直到鼠标停止移动。只有当我用我的Rocket Kone XTD鼠标移动光标时才会发生这种情况。当我使用我的触控板时,一切都很好。

If I move my mouse inside a javax.swing.JFrame, the javax.swing.Timer stops calling the actionPerformed() method until the mouse stops moving. It only occurs when I move the cursor with my Rocket Kone XTD mouse. When I use my trackpad everything is fine.

我该如何解决?我正在使用macOS Sierra。

How can I fix it? I'm using macOS Sierra.

这是我的代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.Timer;

public class Mouse {
    public static void main(String[] args) {
        JFrame frame = new JFrame();

        frame.setSize(500, 500);
        frame.setVisible(true);

        Timer timer = new Timer(10, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("foo");
            }
        });

        timer.start();
    }
}

要了解我在说什么:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Mouse {
    public static void main(String[] args) {
        // Note: Swing/AWT GUIs should be started on the EDT!
        // If the problem displayed here, that is first change I'd make to code.
        final JFrame frame = new JFrame();

        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        Timer timer = new Timer(10, new ActionListener() {

            long lastTime = 0;

            @Override
            public void actionPerformed(ActionEvent e) {
                long nowTime = System.currentTimeMillis();
                long difference = nowTime-lastTime;
                lastTime = nowTime;
                Rectangle r = frame.getBounds();
                Point p = MouseInfo.getPointerInfo().getLocation();
                System.out.println(String.format("%1s\t%2s", 
                        difference, r.contains(p)));
            }
        });

        timer.start();
    }
}



输出:



如果鼠标没有移动,输出看起来像这样:
11 true
13 true
13 true
10 true
12 true
13 true
12 true
13 true
10 true。

Output:

If the mouse is not moving, the output it looks like this: 11 true 13 true 13 true 10 true 12 true 13 true 12 true 13 true 10 true.

当鼠标移动(快速)时,没有输出。当鼠标停止移动时,输出为:2406为真(取决于我移动鼠标的时间长度)。

While the mouse is moving (fast) there is no output. When the mouse stops moving the output is: 2406 true (depending how long I moved my mouse).

如果鼠标移动缓慢,输出如下:真实
5真实
8真实
16真实
4真实
11真实
16真实
44真实
11真
28 true
48 true
77 true
11 true
7 true
15 true
8 true
9 true
12 true
24 true
13 true
4 true
12 true
32 true
13 true
8 true
8 true
13 true
10 true
15 true。

If the mouse is moving slowly, the output looks like this: 17 true 5 true 8 true 16 true 4 true 11 true 16 true 44 true 11 true 28 true 48 true 77 true 11 true 7 true 15 true 8 true 9 true 12 true 24 true 13 true 4 true 12 true 32 true 13 true 8 true 8 true 13 true 10 true 15 true.

推荐答案

我解决了这个问题,因为我将鼠标的轮询率从1000Hz降低到500Hz。现在一切都很完美。我认为问题是UI-Thread过度扩展处理每秒1000次民意调查所以忙于处理计时器

I solved the problem as I reduced the polling-rate of my mouse from 1000Hz to 500Hz. Now everything works perfect. I think the problem was that the UI-Thread was overextended handling the 1000 polls per second so it was to busy to handle the Timer.

这篇关于当鼠标在JFrame内移动时,Swing Timer停止调用actionPerformed()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆