我正在尝试使用下面给出的功能在我的 Java FX GUI 中显示同步时钟。
这还包括一个计时器功能,如果Timer=true
private Calendar cal;
private int minute;
private int hour;
private int second;
private String am_pm;
private boolean Timer;
private Integer tseconds;
private void startClock() {
Timeline clock = new Timeline(new KeyFrame(Duration.millis(Calendar.getInstance().get(Calendar.MILLISECOND)), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
cal = Calendar.getInstance();
second = cal.get(Calendar.SECOND);
minute = cal.get(Calendar.MINUTE);
hour = cal.get(Calendar.HOUR);
am_pm = (cal.get(Calendar.AM_PM) == 0) ? "AM" : "PM";
time.setText(String.format("%02d : %02d : %02d %s", hour, minute, second, am_pm));
if (Timer) {
if (tseconds == 0) {
Timer = false;
//timer.setText("Time Out");
} else {
//timer.setText(tseconds.toString());
tseconds--;
}
}
}
}), new KeyFrame(Duration.millis(Calendar.getInstance().get(Calendar.MILLISECOND))));
clock.setCycleCount(Animation.INDEFINITE);
clock.play();
}
我对此进行了几次测试,发现时钟多次以可变速度更新。
解决了
寻找下面的解决方案。