无法阻止javax swing计时器

时间:2016-05-22 20:52:35

标签: java swing

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

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

}

}

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

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

如,

    Chrono1 cn = new Chrono1(chrono);

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

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

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

例如,进行以下更改:

public function update(Request $request, $id)
    {
       $this->validate($request, array (
            'post' => '',
            'mailbox' => '',
            'conum' => '',
            'prefix' => '',
            'telans' => '',
            'TC' => 'required',

        ));

        //store
        $post = Customers::find($id);

        dd($id);

        $post->post = $request->input('post');
        $post->postpro = $request->input('mailbox');
        $post->telans = $request->input('telans');
        $post->conum = $request->input('conum');
        $post->prefix = $request->inut('prefix');
        $post->tc = $request->input('TC');

        //save
        $post->save();

        //session flash message
        //Session::flash('success','This customer has now been added');

        //redirect
return redirect('/home');}