我有一个 Java Swing 计时器,它每秒更新一个标签。启动计时器后,标签每秒更新一次,一切正常。
然后,在执行之间发生变化的随机时间间隔之后,标签停止更新。我在计时器更新代码中放置了一个断点,它不再被触发。
我还在所有通常停止计时器的地方放置了日志语句,但这些地方都没有被调用。
可能是什么问题?
编辑:这是代码示例
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent arg0) {
secondsRemaining--;
System.out.println("Seconds remaining:" + secondsRemaining);
//update the progressbar
double initialLength = currentSettings.getLength()*60;
double progress = (initialLength - secondsRemaining)/initialLength ;
progressBarTime.setProgress(progress);
//update the progress label
progressPercentage.setText(((int)(progress * 100)) + "%");
if (secondsRemaining >= 0) {
updateTimeRemaining(secondsToString(secondsRemaining));
} else {
System.out.println(">0 seconds TIMER STOPPED with the number of seconds = " + secondsRemaining);
treatmentTimer.stop();
// set the status to Finished
currentState = State.FINISHED;
}
}
}
以及定时器初始化:
tTimer = new Timer(1000, actionListener);
tTimer.start();
奇怪的是,该程序在安装了 JRE 7u7 的 PC 上运行良好,即计时器成功更新了标签,但我在两台装有 7u10 的 PC 上尝试过,并且都发生了计时器停止问题。
最佳答案
可能会抛出异常,请使用try catch或Use UncaughtExceptionHandler进行跟踪。
关于Java定时器在随机时间后不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14265031/