简体   繁体   English

摆动计时器同步

[英]Swing timer synchronization

I'm confused about how the Swing timer work. 我对Swing计时器的工作方式感到困惑。 In the code below, I want to display from 0~9 every 400ms in the first text field when press START (once). 在下面的代码中,我想按START(一次)时,在第一个文本字段中每400ms从0〜9显示一次。 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();
        }           
    });     
}
}

However, it first displays "Finished" before finishing displaying 0 ~ 9. I think the Swing timer works also in EDT, so "tTest.setText("Finished");" 但是,它先显示“ Finished”,然后再显示0〜9。我认为Swing计时器在EDT中也起作用,所以“ 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"? 我如何等待显示0〜9然后打印“完成”? 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();

How to let doOthers() execute after all the doSomething()? 在所有doSomething()之后如何让doOthers()执行? (In some cases, we cannot put doOthers() inside actionPerformed function, as some answers mentioned). (在某些情况下,我们无法将doOthers()放入actionPerformed函数中,如所提到的某些答案)。

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. 因此启动了计时器,然后将文本设置为finish,然后计时器启动并出现第一个数字。

To make the timer display finished after it is finished, put the tTest.setText("Finished"); 要使计时器在完成后显示完成,请放置tTest.setText("Finished"); in the else clause of if(i<10) . if(i<10)else子句中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM