在 JavaFX 中同步时间轴时钟的最佳方法是什么?

What is the best way to synchronize a Timeline clock in JavaFX?

我正尝试在我的 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();
}

我测试了几次,发现很多时候时钟更新都是变速的。

已解决

在下面寻找解决方案。

使用 AnimationTimer 监听更新:

class MyClass extends AnimationTimer {
   private final static long INTERVAL = 1000L;
   private long nextUpdate;
   public void handle(long now) { 
      if (now >= nextUpdate) {
        ....
        nextUpdate = now + INTERVAL;
   }
}

...
MyClass foo = new MyClass(); 
foo.start();

问题已解决。

我将持续时间设置为当前毫秒而不是下一秒剩余的毫秒数。

只有第一个关键帧应该有持续时间(1000-当前毫秒),因为关键帧将运行持续时间等于当前秒剩余的毫秒数。

以下关键帧将 运行以 1 秒的间隔出现。

private void startClock() {
    Timeline clock = new Timeline(new KeyFrame(Duration.millis(1000 - 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.seconds(1)));
    clock.setCycleCount(Animation.INDEFINITE);
    clock.play();
}