一个java的定时器,每隔1秒钟刷新一次JLabel,5秒钟后退出系统。
private void autoExit() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
int MAX_COUNTER = 5;
int counter = 0;
@Override
public void run() {
if (MAX_COUNTER == counter) {
if (!isExitCB.isSelected()) {
System.exit(0);
} else {
this.cancel();
}
}
willExitLabel.setText("System will exit within "
+ (MAX_COUNTER - counter) + " seconds");
counter++;
}
}, 0, 1000);
}