JavaFx timeline-设置初始延迟
我最近开始使用JavaFx timeline-设置初始延迟,java,javafx-2,Java,Javafx 2,我最近开始使用javafx2.0,也许我的问题很基本,但目前我不知道如何解决它。例如,假设我有一个名为时钟的小型演示应用程序: import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javaf
javafx2.0
,也许我的问题很基本,但目前我不知道如何解决它。例如,假设我有一个名为时钟的小型演示应用程序:
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBoxBuilder;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.util.Duration;
import java.util.Date;
public class ClockDemo extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
final Label label = new Label(new Date().toString());
label.setFont(new Font("Arial", 18));
final Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(5), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
label.setText(new Date().toString());
}
}));
timeline.setCycleCount(Timeline.INDEFINITE);
Button button = new Button("Start");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
timeline.play();
}
});
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent windowEvent) {
timeline.stop();
}
});
HBox hBox = HBoxBuilder.create()
.spacing(5.0)
.padding(new Insets(5, 5, 5, 5))
.children(label, button)
.build();
Scene scene = new Scene(hBox, 330, 30);
stage.setScene(scene);
stage.setTitle("Clock demo");
stage.show();
}
}
导入javafx.animation.KeyFrame;
导入javafx.animation.Timeline;
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.geometry.Insets;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.HBoxBuilder;
导入javafx.scene.text.Font;
导入javafx.stage.stage;
导入javafx.stage.WindowEvent;
导入javafx.util.Duration;
导入java.util.Date;
公共类ClockDemo扩展了应用程序{
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
public void start(Stage)引发异常{
最终标签=新标签(新日期().toString());
label.setFont(新字体(“Arial”,18));
最终时间线=新时间线(新关键帧(持续时间.秒(5),新事件处理程序(){
@凌驾
公共无效句柄(ActionEvent ActionEvent){
label.setText(new Date().toString());
}
}));
timeline.setCycleCount(timeline.unfinite);
按钮按钮=新按钮(“开始”);
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent ActionEvent){
timeline.play();
}
});
stage.setOnCloseRequest(新的EventHandler(){
@凌驾
公共无效句柄(WindowEvent WindowEvent){
timeline.stop();
}
});
HBox HBox=HBoxBuilder.create()
.间距(5.0)
.衬垫(新插图(5,5,5,5))
.儿童(标签、按钮)
.build();
场景=新场景(hBox,330,30);
舞台场景;
舞台剧集标题(“时钟演示”);
stage.show();
}
}
基本上,如果您单击“开始”按钮,
时间线
将每隔5秒更新一次标签中的时间。但我面临的问题是,当我单击开始按钮时,我必须等待5秒钟,直到时间线开始运行并更新标签中的时间。那么,有没有办法消除这个初始延迟时间呢?提前感谢。我遇到了一个相同的问题,我通过添加一个关键帧和持续时间来解决它。在时间线的开始处为零,并将我的动作添加到它,第二个关键帧负责延迟
final Timeline timeline = new Timeline(new KeyFrame(Duration.ZERO, new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
label.setText(new Date().toString());
}
}) , new KeyFrame(Duration.seconds(5)));
final Timeline Timeline=新的时间线(新的关键帧(Duration.ZERO,new EventHandler)(){
@凌驾
公共无效句柄(ActionEvent ActionEvent){
label.setText(new Date().toString());
}
}),新的关键帧(持续时间。秒(5));
您可以通过在相关关键帧之前跳到(1毫秒,如果精度不是很重要)来转发时间轴:
timeline.playFrom(Duration.millis(4999));