[英]How to use Timeline to perform a void method every x seconds on JavaFX?
The problem:问题:
I'm trying to use Timeline to call a void method action() every 0.5 seconds.我正在尝试使用 Timeline 每 0.5 秒调用一次 void 方法 action() 。 I have researched how to do this and looked at similar questions on this site, but none are working out for me.
我已经研究了如何做到这一点,并在这个网站上查看了类似的问题,但没有一个对我有用。
What I have tried #1:我尝试过的#1:
Duration sec = Duration.ofSeconds((long) 0.5);
this.timeline = new Timeline(new KeyFrame(sec, e -> {
action();
}));
The error the above caused: "The constructor KeyFrame(Duration, ( e) -> {}) is undefined".上述导致的错误:“构造函数 KeyFrame(Duration, ( e) -> {}) 未定义”。
What I have tried #2:我尝试过的#2:
this.timeline = new Timeline(new KeyFrame(Duration.ofSeconds((long) 0.5), new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event) {
action();
}
}));
The error the above caused: "The constructor KeyFrame(Duration, new EventHandler(){}) is undefined".上述导致的错误:“构造函数 KeyFrame(Duration, new EventHandler(){}) 未定义”。
Thanks in advance for any help.提前感谢您的帮助。
Based on this line:基于此行:
Duration sec = Duration.ofSeconds((long) 0.5);
Note: Casting 0.5
to a long
will simply give you 0
.注意:将
0.5
转换为long
只会给你0
。
You are using the wrong Duration
class.您使用了错误的
Duration
class。 The above indicates you're using java.time.Duration
when you need to be using javafx.util.Duration
.以上表明当您需要使用
java.time.Duration
时,您正在使用javafx.util.Duration
。 Remove the import statement for the former and replace it with one for the latter.删除前者的 import 语句,并用后者替换它。 Then change the above line to:
然后将上面的行更改为:
Duration sec = Duration.seconds(0.5);
Notice the static method for the JavaFX Duration
class is named seconds
, not ofSeconds
.注意 JavaFX
Duration
class 的 static 方法被命名为seconds
,而不是ofSeconds
。 Also, if you want the Timeline
to repeatedly call your method every 0.5
seconds then you need to set its cycle count to Animation#INDEFINITE
.此外,如果您希望
Timeline
每0.5
秒重复调用您的方法,则需要将其循环计数设置为Animation#INDEFINITE
。
Here's an example:这是一个例子:
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
public class App extends Application {
private Label label;
private int count;
@Override
public void start(Stage primaryStage) {
label = new Label();
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0.5), e -> incrementLabel()));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.playFromStart();
primaryStage.setScene(new Scene(new StackPane(label), 500, 300));
primaryStage.show();
}
private void incrementLabel() {
label.setText(String.format("%,d", count++));
}
}
Note: "Incrementing" the text of the Label
could be done directly in the EventHandler
, but I moved it to a void
method to better fit your question.注意: “增加”
Label
的文本可以直接在EventHandler
中完成,但我将其移至void
方法以更好地满足您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.