繁体   English   中英

无法阻止javax swing计时器

[英]unable to stop javax swing timer

我试图阻止我的计时器有问题。 我有两个不同的类,第一个包含启动和停止按钮的按钮,第二个包含计时类。 这是按钮:

btnStopstart = new JButton("START");
btnStopstart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {


    Chrono1 cn = new Chrono1(chrono);
    String texte = btnStopstart.getText();
    if(texte.equals("START")){
        btnStopstart.setText("STOP");
        try {
            cn.Editchrono(texte);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }else if(texte.equals("STOP")){
        btnStopstart.setText("START");
        cn.Editchrono(texte);
    }

}

});

这是时间类:

public class Chrono1 {
private static int sec;
private JTextField chrono;
public Chrono1(JTextField chrono){
    this.chrono = chrono;
}
public void Editchrono(String txt){
    /* Le timer */
    int delais=1000;
    ActionListener tache_timer;
    tache_timer = new ActionListener(){
        public void actionPerformed(ActionEvent e){
            sec++;

            if(sec == 15 ){
                //Conditions
            }
            if(sec == 16){
                /*On realise une pause de 1 sec */
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                //mettre les conditions ici
                sec = 0;

            }
            //System.out.println(sec);

            chrono.setText("" + sec);
        }
    };
    final Timer timer1= new Timer(delais,tache_timer);
    if(txt.equals("START")){
        timer1.start();
    }else if(txt.equals("STOP")){
        timer1.stop();
        //sec = 0;
    }

}

}

感谢您的帮助。

你可以在Swing Timer上调用stop() ,但不能在正在运行的Timer实例上调用。 而是在一个全新的Timer实例上调用stop(),一个甚至没有运行的实例,以及一个与实际运行的Timer完全无关的实例。 您需要为Chrono1类提供一个Timer字段,比如称为timer ,将此字段设置为在启动时引用正在运行的Timer,并在调用stop时调用此字段上的stop(如果不为null)。 您还需要创建一个且仅一个Chrono1对象。

例如,

public class Chrono1 {
    private static int sec;
    private JTextField chrono;
    private Timer timer1; // ***** added ***

    public Chrono1(JTextField chrono){
        this.chrono = chrono;
    }

    public void Editchrono(String txt){
        int delais=1000;
        ActionListener tache_timer;
        tache_timer = new ActionListener(){
            public void actionPerformed(ActionEvent e){

                // .... etc.....

            }
        };

        if(txt.equals("START")) {

            // **** note changes? ****
            // final Timer timer1= new Timer(delais,tache_timer);  // ** no **
            timer1= new Timer(delais,tache_timer);  // ** yes! **
            timer1.start();
        }else if(txt.equals("STOP")){
            if (timer1 != null && timer1.isRunning()) {
                timer1.stop();
            }
        //sec = 0;
        }
    }
}

也不应该重新创建这个人:

    Chrono1 cn = new Chrono1(chrono);

因此,这应该是整个类或内部ActionListener类的私有实例字段,而不是每次按下按钮都重新创建。

例如,进行以下更改:

btnStopstart.addActionListener(new ActionListener(){
    private Chrono1 cn = new Chrono1(chrono); // **** add this 

    public void actionPerformed(ActionEvent e) {
        // Chrono1 cn = new Chrono1(chrono); // **** remove this 
        String texte = btnStopstart.getText();
        if(texte.equals("START")){
            btnStopstart.setText("STOP");
            try {
                cn.Editchrono(texte);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } else if(texte.equals("STOP")) {
            btnStopstart.setText("START");
            cn.Editchrono(texte);
        }
    }
});    

暂无
暂无

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

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