【问题标题】:JavaFx timeline - set initial delayJavaFx 时间线 - 设置初始延迟
【发布时间】:2013-08-20 16:24:29
【问题描述】:

我最近开始使用JavaFx 2.0,也许我的问题很基本,但目前我不知道如何解决它。例如,假设我有一个名为 Clock 的小型演示应用程序:

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();
    }
}

基本上,如果您单击开始按钮,Timeline 将每 5 秒更新一次 Label 时间。但是我面临的问题是,当我单击开始按钮时,我必须等待 5 秒钟,直到 Timeline 开始运行并更新 Label 中的时间。那么,有没有什么办法可以消除这个初始延迟时间呢?

【问题讨论】:

    标签: java javafx-2


    【解决方案1】:

    我遇到了同样的问题,我通过在Duration.ZERO 的开头添加KeyFrame 并在timeline 的开头添加我的操作来解决它,第二个KeyFrame 负责延迟。

    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)));
    

    【讨论】:

      【解决方案2】:

      您可以通过跳转到相关关键帧之前(1 毫秒,如果准确性不那么重要)来转发时间线:

      timeline.playFrom(Duration.millis(4999));
      

      【讨论】:

        猜你喜欢
        • 2011-11-17
        • 1970-01-01
        • 1970-01-01
        • 2012-02-08
        • 2020-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多