且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JavaFX实时时间和日期

更新时间:2022-05-04 22:59:33

我认为你需要FX UI线程 Platform.runLater(...)为此,你可以使用 时间轴

I think you need FX UI Thread Platform.runLater(...) for that, but you can do something like this using Timeline in you controller class,

@FXML
public void initialize() {

    Timeline clock = new Timeline(new KeyFrame(Duration.ZERO, e -> {        
        second = LocalDateTime.now().getSecond();
        minute = LocalDateTime.now().getMinute();
        hour = LocalDateTime.now().getHour();
        time.setText(hour + ":" + (minute) + ":" + second);
    }),
         new KeyFrame(Duration.seconds(1))
    );
    clock.setCycleCount(Animation.INDEFINITE);
    clock.play();
}