【问题标题】:How to get JavaFX Timeline to increase by second and to bind to a Progressbar?如何让 JavaFX 时间线增加一秒并绑定到进度条?
【发布时间】:2016-08-05 01:12:38
【问题描述】:

我正在尝试创建一个系统,在该系统中,时间线用于记录一分钟的持续时间。在这种情况下,我希望时间线每秒递增,并让进度条记录到 1 分钟,然后再调用操作并重置时间线。

final Timeline timeline = new Timeline();
timeline.setCycleCount(Animation.INDEFINITE);
timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(1)));
progress.progressProperty().bind(timeline.getTotalDuration()???);
timeline.play();

我已将循环计数设置为无限期,这样时间线循环就不会停止。我也尝试将 Progressbar 属性绑定到时间线,但它需要一个 Observablevalue 来执行此操作,而 timeline#getTotalDuration 不提供。

我尝试在没有绑定的情况下在单独的线程上运行程序:

new Thread(new Task<Void>() {
            @Override
            protected Void call() throws Exception {
                while(true){
                    System.out.printf("s:\t%f\n",timeline.getTotalDuration().toSeconds());
                    Thread.sleep(1000);
                }
            }
}).start();

但是,运行它不会增加每秒的计时器,而是一次完成所有周期。由于我设置的cycleCount是INDEFINITE,所以#totalDuration的结果是infinite

如何让时间线以秒为单位工作,如何将持续时间绑定到进度条直到达到 100%,以便可以调用我的特殊操作?

【问题讨论】:

    标签: java javafx


    【解决方案1】:

    如果您只想让进度条在一分钟内从零重复增加到满,并在每分钟结束时执行代码,您只需要:

        ProgressBar progress = new ProgressBar();
        Timeline timeline = new Timeline(
            new KeyFrame(Duration.ZERO, new KeyValue(progress.progressProperty(), 0)),
            new KeyFrame(Duration.minutes(1), e-> {
                // do anything you need here on completion...
                System.out.println("Minute over");
            }, new KeyValue(progress.progressProperty(), 1))    
        );
        timeline.setCycleCount(Animation.INDEFINITE);
        timeline.play();
    

    这将为进度条创建一个“模拟”效果,即它不会每秒递增,而是在整分钟内平滑增加。

    如果你真的想每秒递增,使用IntegerProperty来表示秒数,并绑定进度条的progress属性:

        ProgressBar progress = new ProgressBar();
        IntegerProperty seconds = new SimpleIntegerProperty();
        progress.progressProperty().bind(seconds.divide(60.0));
        Timeline timeline = new Timeline(
            new KeyFrame(Duration.ZERO, new KeyValue(seconds, 0)),
            new KeyFrame(Duration.minutes(1), e-> {
                // do anything you need here on completion...
                System.out.println("Minute over");
            }, new KeyValue(seconds, 60))   
        );
        timeline.setCycleCount(Animation.INDEFINITE);
        timeline.play();
    

    这里的重点是 IntegerProperty 将在 0 到 60 之间进行插值,但只接受整数值(即,它将内插值截断为 int)。

    这是第二版的 SSCCE:

    import javafx.animation.Animation;
    import javafx.animation.KeyFrame;
    import javafx.animation.KeyValue;
    import javafx.animation.Timeline;
    import javafx.application.Application;
    import javafx.beans.property.IntegerProperty;
    import javafx.beans.property.SimpleIntegerProperty;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.ProgressBar;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    import javafx.util.Duration;
    
    public class OneMinuteTimer extends Application {
    
        @Override
        public void start(Stage primaryStage) {
            ProgressBar progress = new ProgressBar();
            progress.setMinWidth(200);
            progress.setMaxWidth(Double.MAX_VALUE);
            IntegerProperty seconds = new SimpleIntegerProperty();
            progress.progressProperty().bind(seconds.divide(60.0));
            Timeline timeline = new Timeline(
                new KeyFrame(Duration.ZERO, new KeyValue(seconds, 0)),
                new KeyFrame(Duration.minutes(1), e-> {
                    // do anything you need here on completion...
                    System.out.println("Minute over");
                }, new KeyValue(seconds, 60))   
            );
            timeline.setCycleCount(Animation.INDEFINITE);
            timeline.play();
    
            StackPane root = new StackPane(progress);
            root.setPadding(new Insets(20));
            primaryStage.setScene(new Scene(root));
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      相关资源
      最近更新 更多