JProgressBar在Linux上缓慢

我正在一个项目中,我有进度条来指示游戏控制器轴的状态(事件发生在单独的线程上)。事件回调有效,但是当我尝试在Linux上使用进度条显示当前值(来自事件)时,它不能很好地工作。在Windows上,运动是平稳的,但是在Linux上,随着进度条的值更改,它似乎停滞了。我整理了一个最小的示例来说明这一点(无需使用本机库来处理游戏手柄)。

import java.awt.BorderLayout;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

public class Test extends JFrame {

    JProgressBar bar;

    public Test() {

        bar = new JProgressBar(0,100);

        setLayout(new BorderLayout());
        add(bar,BorderLayout.NORTH);

        setSize(500,300);
        setLocationRelativeTo(null);

        EventSimulateThread t = new EventSimulateThread();
        t.start();

    }

    public void updateProgress(int value) {
        bar.setvalue(value);
    }

    public static void main(String args[]) {
        Test t = new Test();
        t.setVisible(true);
    }

    class EventSimulateThread extends Thread {
        Random rand = new Random();
        @Override 
        public void run() {
            while(true) {
                for (int i = 0; i <= 100; i++) {
                    final int v = i;
                    SwingUtilities.invokeLater(() -> {
                        updateProgress(v);
                    });
                    try {Thread.sleep(10);}catch(Exception e) {}
                }
                for (int i = 100; i >= 0; i--) {
                    final int v = i;
                    SwingUtilities.invokeLater(() -> {
                        updateProgress(v);
                    });
                    try {Thread.sleep(10);}catch(Exception e) {}
                }
            }
        }
    }
}

在Windows上运行时,进度条会顺利浏览所有值。在linux上它跳来跳去。有什么想法会导致这种情况吗?

编辑:我在使用Gnome3桌面的Ubuntu 18.04上对此进行了测试。在所有测试(Windows和Linux)中,我都在使用Java 11。

xiazai1999 回答:JProgressBar在Linux上缓慢

找到了答案here

看起来像是由于Linux上的opengl默认禁用。可以通过在main中添加以下行来修复。

System.setProperty("sun.java2d.opengl","true");
本文链接:https://www.f2er.com/3164200.html

大家都在问