如何停止javax.swing.Timer中?(How to Stop Javax.Swing.ti

2019-10-19 06:13发布

我是一个Java新手,我创建使用NetBeans调度程序。 我的软件将获得时间和用户的动作,并在分离的ArrayList输入这些数据。 然后我用javax.swing.Timer中开始倒计时。 当用户完成输入数据并点击“运行”按钮,计时器会从数组列表的第一个元素得到的运行时间,并开始倒计时。 当倒数时间到达0时,计时器会得到从阵列列表中的下一个元素的倒计时时间,等等。

我的问题是,定时器运行非常好,但是当有ArrayList中没有更多的元素,它仍然不会停止。 请帮我解决一下这个。 这里是我的代码,我遇到问题的一部分:

private void btnstartRunActionPerformed(java.awt.event.ActionEvent evt) {                                            
  countdown = ActionTime.get(0);
  Action.setText(ActionName.get(c));
  timer = new Timer(1000,new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent event){

      if(countdown<=0){
          try{
            c++;
            Action.setText(ActionName.get(c));
            //this nested if statement is to check whether the array has any more elements.
            //(This is the part that doesn't work.)
            if(c >= ActionTime.size()){
              timer.stop();
              JOptionPane.showMessageDialog(null, "Workout Completed!");
            }  
            countdown=ActionTime.get(c);
            timer.restart();

          } catch (Exception error2){
          }
       }
       Time.setText(Integer.toString(countdown));
       countdown--;
     }
  });
  timer.start();
}                                           

Answer 1:

你应该改变:

            c++;
            Action.setText(ActionName.get(c)); //move this down
            if(c>=ActionTime.size()){
                timer.stop();
                JOptionPane.showMessageDialog(null, "Workout Completed!");
            }

至:

            c++;
            if(c>=ActionTime.size()){
                timer.stop();
                JOptionPane.showMessageDialog(null, "Workout Completed!");
                return; //add this too as the program should not continue with below
            }
            Action.setText(ActionName.get(c)); //to here

因为当你试图做ActionName.get(c)时的值c超过最大可能值时, get方法将抛出Exception 。 这使我们对你正在做的第二个错误:

           catch(Exception error2){

           }

这样做可以使程序忽略Exception并继续,仿佛什么都没有发生。 一个简单的对做错误,欺骗了你以为别的东西是错的,但是你现在知道! 将其更改为:

           catch(Exception error2){
                error2.printStackTrace();
                System.exit(-1); //or some error handling
           }

或删除try/catch块,这样的Exception会结束你的程序,或者处理一些其他的方式。



Answer 2:

这里是一个例子:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.Timer;

public class Main extends JFrame {
    Timer timer;

    int counter;

    Main(String title) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ActionListener a = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("Counter = " + counter);

                if (++counter > 10) {
                    timer.stop();
                    System.exit(0);
                }
            }
        };

        timer = new Timer(300, a);
        timer.start();

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new Main("Timer Demo1");
    }
}


文章来源: How to Stop Javax.Swing.timer?
标签: java swing timer