作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想制作一个秒表(小时:分钟:秒),我想我应该按以下方式进行。变量结果将传递到我的 TextField
中,startTime
将是我们启动秒表的时间,传递的时间将是当前时间(常量)
result=startTime-elapsedTime;
但我不知道要使用哪个日期类,也不知道如何重复此操作。
result=startTime-elapsedTime;
每秒更新 TextField。
欢迎所有建议。
最佳答案
您可以使用 TimeLine
动画来更新 TextField
:
public void startTimer() {
timeline = new Timeline(
new KeyFrame(Duration.seconds(0),
e ->advanceDuration()),
new KeyFrame(Duration.seconds(1)));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}
private void advanceDuration() {
if (seconds < 59) {
seconds++;
} else {
seconds = 0;
if (minutes < 59) {
minutes++;
}else{
minutes = 0;
hours++;
}
}
updateDisplay();
}
编辑:
根据评论,这里是使用 AnimationTimer
的解决方案:
public class Timer extends Application {
private AnimationTimer timer;
private Label lblTime = new Label("0 .s");
private int seconds;
@Override
public void start(Stage primaryStage) {
timer = new AnimationTimer() {
private long lastTime = 0;
@Override
public void handle(long now) {
if (lastTime != 0) {
if (now > lastTime + 1_000_000_000) {
seconds++;
lblTime.setText(Integer.toString(seconds) + " .s");
lastTime = now;
}
} else {
lastTime = now;
}
}
@Override
public void stop() {
super.stop();
lastTime = 0;
seconds = 0;
}
};
Button btnStart = new Button("START");
btnStart.setOnAction(e ->
{
lblTime.setText("0 .s");
timer.start();
});
Button btnStop = new Button("STOP");
btnStop.setOnAction(e -> timer.stop());
VBox box = new VBox(16, lblTime, btnStart, btnPause);
box.setAlignment(Pos.CENTER);
primaryStage.setScene(new Scene(new StackPane(box)));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
关于java - 跑表。数秒。 javafx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38717690/
我想制作一个秒表(小时:分钟:秒),我想我应该按以下方式进行。变量结果将传递到我的 TextField 中,startTime 将是我们启动秒表的时间,传递的时间将是当前时间(常量) result=s
我是一名优秀的程序员,十分优秀!