JavaFX:将时间轴的持续时间绑定到属性

时间:2013-10-23 18:43:13

标签: java javafx-2

我的应用程序有一个带有一些double值的组合框。用户可以选择任何值。该应用程序有一个" TimeLine"附加到它将在控制台上打印一个语句。 sscce位于下方。应该发生的是应该打印从组合框中选择的选项的文本。请指教。

package just.to.test;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class TimerSample extends Application  {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Text Fonts");

        Group g = new Group();
        Scene scene = new Scene(g, 150, 100);

        ObservableList<Double> data = FXCollections.observableArrayList();

        data.add(5.0);
        data.add(10.0);
        data.add(15.0);
        data.add(20.0);

        ComboBox<Double> timeOptions = new ComboBox<Double>(data);
        timeOptions.getSelectionModel().selectFirst();

        g.getChildren().addAll(timeOptions);

        primaryStage.setScene(scene);
        primaryStage.show();

        final double timerInterval = timeOptions.getSelectionModel().getSelectedItem();

        KeyFrame keyFrame = new KeyFrame(Duration.seconds(timerInterval),
            new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    System.out.println("This is called every "
                        + timerInterval + " seconds");
                }
            });

        Timeline timerThread = new Timeline(keyFrame);
        timerThread.setCycleCount(Timeline.INDEFINITE);
        timerThread.play();
    }
}

2 个答案:

答案 0 :(得分:7)

您无法将时间轴的持续时间绑定到属性,因为时间轴中的关键帧的持续时间不是属性,您只能将属性绑定到属性。

您需要做的是侦听组合框值的更改,并在用户选择新的持续时间时触发创建具有新持续时间的新关键帧。您也无法修改正在运行的时间轴的关键帧,因此您必须在设置时间轴的新关键帧之前停止时间轴,然后在设置新关键帧后启动时间轴。

示例代码

import javafx.animation.*;
import javafx.application.Application;
import javafx.beans.value.*;
import javafx.collections.*;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.control.ComboBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class TimerSample extends Application  {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) {
        Group g = new Group();
        Scene scene = new Scene(g, 150, 100);

        ComboBox<Double> timerOptions = createTimerOptions(
                0.5, 1.0, 1.5, 2.0
        );
        g.getChildren().addAll(timerOptions);

        createTimer(timerOptions);

        stage.setScene(scene);
        stage.show();
    }

    private ComboBox<Double> createTimerOptions(double... options) {
        ObservableList<Double> data = FXCollections.observableArrayList();

        for (Double option: options) {
            data.add(option);
        }

        return new ComboBox<Double>(data);
    }

    private void createTimer(ComboBox<Double> timeOptions) {
        final Timeline timer = new Timeline();
        timer.setCycleCount(Timeline.INDEFINITE);

        timeOptions.valueProperty().addListener(new ChangeListener<Double>() {
            @Override
            public void changed(ObservableValue<? extends Double> observable, Double oldValue, Double newValue) {
                resetTimer(timer, newValue);
            }
        });

        timeOptions.getSelectionModel().selectFirst();
    }

    private void resetTimer(Timeline timer, final double timerInterval) {
        KeyFrame keyFrame = new KeyFrame(
            Duration.seconds(timerInterval),
            new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    System.out.println(
                            "This is called every "
                                    + timerInterval
                                    + " seconds"
                    );
                }
            }
        );

        timer.stop();
        timer.getKeyFrames().setAll(
                keyFrame
        );
        timer.play();
    }
}
  

只是想知道性能是否明显更重,因为在组合框值的每次更改时添加一个新的关键帧。

在这种情况下,不要过于担心性能 - 这是一种高效的操作。

除了创建新的关键帧之外,没有其他解决方案,因为关键帧是immutable objects

你可以在前面构建关键帧,或者在你懒惰地构建它们时将它们放在像LRU cache这样的东西上,但总的来说,所涉及的额外复杂性几乎肯定不值得。

答案 1 :(得分:4)

所有动画都有rateProperty(),可以在正在运行的动画中更改

这似乎是一个更清洁的解决方案:

private void createTimer(ComboBox<Double> timeOptions) {
    Timeline timer = new Timeline(
            new KeyFrame(Duration.seconds(1),
            evt-> System.out.println(
                        "This is called every "
                                + timeOptions.getValue()
                                + " seconds"
                ));

    timer.setCycleCount(Timeline.INDEFINITE);
    timer.rateProperty()
         .bind(new SimpleDoubleProperty(1.0)
         .divide(timeOptions.valueProperty()));

    timeOptions.getSelectionModel().selectFirst();
}
相关问题