我是一个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();
}