Swing计时器同步 [英] Swing timer synchronization

查看:160
本文介绍了Swing计时器同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Swing计时器的工作原理感到困惑。在下面的代码中,我想在按START(一次)时在第一个文本字段中每隔400ms显示0~9。之后,第二个文本字段将显示已完成。

I'm confused about how the Swing timer work. In the code below, I want to display from 0~9 every 400ms in the first text field when press START (once). After that the second text field will display "Finished".

public class Main extends JPanel{

private static final long serialVersionUID = 1L;
private JButton bStart;
private JTextField tTest;
private JTextField tNumber;

Main(){
    bStart = new JButton("Start");
    bStart.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            displayNumbers();
        }       
    });

    tTest = new JTextField(null, 30);
    tNumber = new JTextField(" ", 30);
    tNumber.setEditable(false);
    this.setSize(300, 100);
    this.add(bStart);
    this.add(tNumber);
    this.add(tTest);

}

public void displayNumbers(){
    new Timer(400, new ActionListener() {
        int i = 0;
        public void actionPerformed(ActionEvent evt) {
            if(i<10){
                tNumber.setText(Integer.toString(i));
                i++;
            }       
            else
                ((Timer)evt.getSource()).stop();
        }
    }).start(); 

    tTest.setText("Finished");
}

public static void createAndShowGUI(){
    JFrame frame = new JFrame("test");
    frame.add(new Main());
    frame.setSize(400, 150);
    frame.setVisible(true); 
}


public static void main(String args[]){
    SwingUtilities.invokeLater(new Runnable(){
        @Override
        public void run() {
            // TODO Auto-generated method stub
            createAndShowGUI();
        }           
    });     
}
}

然而,在完成显示之前,它首先显示已完成 0~9。我认为Swing计时器也适用于EDT,所以tTest.setText(Finished);将在计时器线程后执行。为什么不起作用?如何等待完成显示0~9然后打印完成?谢谢!

However, it first displays "Finished" before finishing displaying 0 ~ 9. I think the Swing timer works also in EDT, so "tTest.setText("Finished");" will be executed after timer thread. Why does not it work? How do I wait finishing displaying 0 ~ 9 then print "Finished"? Thanks!

感谢您的回答。事实上,我想问的是:

Thanks for your answers. In fact what I want to ask is in general:

new Timer(delay, new ActionListener() {

    public void actionPerformed(ActionEvent evt) {
            doSomething();
    }

}).start(); 

    doOthers();

如何让doOthers()在所有doSomething()之后执行? (在某些情况下,我们不能将doOthers()放在actionPerformed函数中,如提到的一些答案)。

How to let doOthers() execute after all the doSomething()? (In some cases, we cannot put doOthers() inside actionPerformed function, as some answers mentioned).

推荐答案

计时器同时工作。所以计时器启动,然后文本设置为完成,然后计时器触发并显示第一个数字。

The timer works concurrently. So the timer is started, then the text is set to finished, and then the timer fires and the first number appears.

使计时器显示在完成后完成,将 tTest.setText(已完成); 放入 else 子句中(i< 10)

To make the timer display finished after it is finished, put the tTest.setText("Finished"); in the else clause of if(i<10).

这篇关于Swing计时器同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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