这里重要的是,您需要了解对 UI 进行的任何更新都需要在 UI 的主线程中完成。您还应该避免阻塞 UI 的主线程,这将阻止 UI 更新。
首先,请阅读Concurrency in JavaFX
当您处理时间或持续时间时,您应该始终使用专用库,例如 java.time。为什么?因为实际上一天没有 24 小时,也没有每分钟 60 秒 - 我知道,这很奇怪。
你可以...
使用Timeline
import java.time.LocalDateTime;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Test extends Application {
private LocalDateTime startTime;
private Timeline timer;
private Label time;
@Override
public void start(Stage primaryStage) {
time = new Label("...");
Button btn = new Button();
btn.setText("Start");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if (timer != null) {
timer.stop();
}
startTime = LocalDateTime.now();
timer = new Timeline(new KeyFrame(Duration.millis(500), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
LocalDateTime now = LocalDateTime.now();
java.time.Duration duration = java.time.Duration.between(startTime, now);
time.setText(format(duration));
}
}));
timer.setCycleCount(Timeline.INDEFINITE);
timer.play();
}
});
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.getChildren().add(time);
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
protected String format(java.time.Duration duration) {
long hours = duration.toHours();
long mins = duration.minusHours(hours).toMinutes();
long seconds = duration.minusMinutes(mins).toMillis() / 1000;
return String.format("%02dh %02dm %02ds", hours, mins, seconds);
}
public static void main(String[] args) {
launch(args);
}
}
使用AnimationTimer
这可能是首选的解决方案,因为定时器的调用与动画引擎的帧刷新同步
import java.time.LocalDateTime;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Test extends Application {
private LocalDateTime startTime;
private AnimationTimer timer;
private Label time;
@Override
public void start(Stage primaryStage) {
time = new Label("...");
Button btn = new Button();
btn.setText("Start");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if (timer != null) {
timer.stop();
}
startTime = LocalDateTime.now();
timer = new AnimationTimer() {
@Override
public void handle(long click) {
LocalDateTime now = LocalDateTime.now();
java.time.Duration duration = java.time.Duration.between(startTime, now);
time.setText(format(duration));
}
};
timer.start();
}
});
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.getChildren().add(time);
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
protected String format(java.time.Duration duration) {
long hours = duration.toHours();
long mins = duration.minusHours(hours).toMinutes();
long seconds = duration.minusMinutes(mins).toMillis() / 1000;
return String.format("%02dh %02dm %02ds", hours, mins, seconds);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}