Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可以用摆动计时器以更优雅的方式完成吗?_Java_User Interface_Swing_Timer - Fatal编程技术网

Java 可以用摆动计时器以更优雅的方式完成吗?

Java 可以用摆动计时器以更优雅的方式完成吗?,java,user-interface,swing,timer,Java,User Interface,Swing,Timer,下面是最简单的GUI倒计时代码。使用Swing定时器可以以更短、更优雅的方式完成同样的操作吗 import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; public class CountdownNew { static JLabel label; // Method which defines the appearance of the window.

下面是最简单的GUI倒计时代码。使用Swing定时器可以以更短、更优雅的方式完成同样的操作吗

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class CountdownNew {

    static JLabel label;

    // Method which defines the appearance of the window.   
    public static void showGUI() {
        JFrame frame = new JFrame("Simple Countdown");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        label = new JLabel("Some Text");
        frame.add(label);
        frame.pack();
        frame.setVisible(true);
    }

    // Define a new thread in which the countdown is counting down.
    public static Thread counter = new Thread() {

        public void run() {
            for (int i=10; i>0; i=i-1) {
                updateGUI(i,label);
                try {Thread.sleep(1000);} catch(InterruptedException e) {};
            }
        }
    };

    // A method which updates GUI (sets a new value of JLabel).
    private static void updateGUI(final int i, final JLabel label) {
        SwingUtilities.invokeLater( 
            new Runnable() {
                public void run() {
                    label.setText("You have " + i + " seconds.");
                }
            }
        );
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                showGUI();
                counter.start();
            }
        });
    }

}

您可以通过使用适当的工具使其更加优雅。

您可以通过使用适当的工具使其更加优雅。

是的,使用计时器。updateGUI将是计时器任务的代码,但它需要一些更改,因为您无法为每个调用传递i,因为您只获得了一个run()方法。

是的,使用计时器。updateGUI将是计时器任务的代码,但它需要一些更改,因为您无法为每个调用传递i,因为您只获得了一个run()方法。

是的,您应该使用Swing计时器。您不应该使用util计时器和TimerTask

当Swing计时器触发时,代码在EDT上执行,这意味着您只需要调用label.setText()方法

使用uitl计时器和TimerTask时,代码不会在EDT上执行,这意味着您需要将代码包装在SwingUtilities.invokeLater中,以确保代码在EDT上执行


也就是说,使用摆动计时器的方法比您当前的方法更短、更优雅,它简化了编码,因为to代码是在EDT上执行的。

是的,您应该使用摆动计时器。您不应该使用util计时器和TimerTask

当Swing计时器触发时,代码在EDT上执行,这意味着您只需要调用label.setText()方法

使用uitl计时器和TimerTask时,代码不会在EDT上执行,这意味着您需要将代码包装在SwingUtilities.invokeLater中,以确保代码在EDT上执行

也就是说,使用摆动计时器的方法比当前的方法更短、更优雅,它简化了编码,因为to代码是在EDT上执行的