簡體   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