Swing timer synchronization

自闭症网瘾萝莉.ら 提交于 2019-12-08 07:33:11

问题


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();
        }           
    });     
}
}

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();

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


回答1:


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.

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



来源:https://stackoverflow.com/questions/24003532/swing-timer-synchronization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!