Java Swing - 定时器中的 JTextArea 更新不是常量

Java Swing - JTextArea update in a Timer not constant

我正在使用 Swing 框架开发一个 Java GUI 应用程序,我需要在其中显示一些模拟 LCD 显示器的 JTextArea 中的文本。

文本的每个字符通过 Swing Timer 每 250 毫秒附加一次,但是,如果我没有将鼠标悬停在 JTextArea 上,则更新不是恒定的。

这是我悬停的时候

这是我不悬停的时候

这是一个例子,基于定时器

public class App {

    public static class LCDisplay extends JTextArea {

        public LCDisplay() {
            super(1, 20);
            setEditable(false);
            Font font = new Font(Font.MONOSPACED, Font.PLAIN, 50);
            setFont(font);
            setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
            setForeground(Color.GREEN);
            setBackground(Color.BLUE);
        }

    }

    public static void main(String[] args) {
        LCDisplay display = new LCDisplay();
        display.setText("                    ");
        display.setBorder(BorderFactory.createLineBorder(Color.BLACK, 10, false));

        Function<String, Boolean> setLCDisplayText = text -> {
            int displayTextLength = display.getText().length();
            try {
                display.setText(display.getText(1, displayTextLength - 1));
            } catch (BadLocationException e) {
                e.printStackTrace();
            }
            display.append(text);
            return display.getText().trim().isEmpty();
        };

        StringBuilder sb = new StringBuilder("Colore rosso");
        for(int i=0; i<20; i++) {sb.append(' ');}
        int interval = 250;
        AtomicInteger supIndex = new AtomicInteger(0);
        AtomicLong expected = new AtomicLong(Instant.now().toEpochMilli() + interval);
        Timer slideText = new Timer(0, e -> {
            int drift = (int) (Instant.now().toEpochMilli() - expected.get());

            boolean exceeded = setLCDisplayText.apply(sb.charAt(supIndex.getAndIncrement()) + "");
            if (exceeded) {
                supIndex.set(0);
            }
            Timer thisTimer = (Timer) e.getSource();

            expected.addAndGet(interval);
            thisTimer.setInitialDelay(Math.max(0, interval - drift));
            thisTimer.restart();
        });

        JFrame frame = new JFrame("app");
        frame.add(display);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        slideText.start();
    }

}

可能是我做的不对,但是我真的不知道怎么解决。

提前致谢。

问题是由于 OpenGL 默认情况下在 Linux 中禁用(而在 Windows 中启用)。

感谢,我添加了以下内容属性

System.setProperty("sun.java2d.opengl", "true");

问题解决了