如何停止 Javax.Swing.timer? [英] How to Stop Javax.Swing.timer?

查看:52
本文介绍了如何停止 Javax.Swing.timer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java 新手,正在使用 netbeans 创建调度程序.我的软件将从用户那里获取时间和操作,并将这些数据输入到单独的数组列表中.然后我使用 javax.swing.timer 来启动倒数计时器.当用户完成输入数据并点击运行"按钮时,计时器将从数组列表中的第一个元素开始获取运行时间并开始倒计时.当倒计时时间为 0 时, timer 将从数组列表中的下一个元素开始获取倒计时时间,依此类推.

I'm a java newbie and I'm creating a scheduler program using netbeans. My software will get Time and action from the user and enter those data in to separate arraylists. I've then used javax.swing.timer to start a countdown timer. When user finished entering data and hit the "run" button,The timer will get the running time from the first element in the array list and start counting down. when the countdown time reach 0 , timer will get the count down time from the next element in the array list, and so on.

我的问题是,计时器运行得很好,但是当数组列表中没有更多元素时,它仍然不会停止.请帮我解决一下这个.这是我遇到问题的代码部分:

My problem is, the timer runs perfectly but when there is no more elements in the arraylist, it still won't stop. Please help me with this. Here's the part of my code I'm having trouble with:

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

推荐答案

你应该改变:

            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.这导致我们犯了第二个错误:

Because when you attempt to do ActionName.get(c) when the value of c exceeds the maximum possible value, the get method will throw an Exception. That leads us to the second mistake you're making:

           catch(Exception error2){

           }

这样做会使程序忽略Exception 并继续像什么也没发生一样.一个容易犯的错误,使您误以为其他事情是错误的,但现在您知道了!改为:

Doing this makes the program ignore the Exception and continue as though nothing happened. An easy-to-make mistake that deceived you into thinking something else is wrong, but now you know! Change it to:

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

或者删除 try/catch 块,以便 Exception 将结束您的程序或以其他方式处理.

Or remove the try/catch block so that the Exception will end your program or is handled some other way.

这篇关于如何停止 Javax.Swing.timer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆