Javafx Timeline获取当前周期时间

Javafx Timeline获取当前周期时间,javafx,timeline,Javafx,Timeline,我使用的是javaFX,目前仍停留在这一点上: Timeline timeline = new Timeline(); timeline = new Timeline(new KeyFrame(Duration.ZERO, new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent actionEvent) { System.out

我使用的是javaFX,目前仍停留在这一点上:

Timeline timeline = new Timeline();
    timeline = new Timeline(new KeyFrame(Duration.ZERO, new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent actionEvent) {
            System.out.println("Counting");
            //myFunction(currentCycleStep) <------
        }
    }), new KeyFrame(Duration.seconds(SimulationController.speed)));


    timeline.setCycleCount(5);
    timeline.play();
时间线=新时间线();
时间线=新时间线(新关键帧(Duration.ZERO,new EventHandler()){
@凌驾
公共无效句柄(ActionEvent ActionEvent){
系统输出打印项次(“计数”);

//myFunction(currentCycleStep)我认为没有直接的方法可以从时间线中获得实际的周期

您可以为此编写自己的属性。 下面是一个示例,它监视currentTime,并在每次更改“方向”时递增我们自己创建的属性的值

例如:

autoReverse=false

currentTime: 1s
currentTime: 2s
currentTime: 3s
currentTime: 0s  <--------- increment actual cycle
currentTime: 1s
currentTime: 1s
currentTime: 2s
currentTime: 3s
currentTime: 2s  <--------- increment actual cycle
currentTime: 1s
当前时间:1s
当前时间:2秒
当前时间:3s
当前时间:0s{
System.out.println(newValue);
});
timeline.play();
}
@Override
public void start(Stage primaryStage) throws Exception {
    SimpleStringProperty testProperty = new SimpleStringProperty();
    Timeline timeline = new Timeline(new KeyFrame(Duration.millis(1000), new KeyValue(testProperty, "1234")));
    timeline.setCycleCount(Timeline.INDEFINITE);

    // ----------------------------------------------------------------------
    // we create our own property for the actual cycle-index
    // ----------------------------------------------------------------------
    SimpleIntegerProperty actualCycleProperty = new SimpleIntegerProperty(0);
    timeline.currentTimeProperty().addListener((observable, oldValue, newValue) -> {
        boolean smaller = newValue.toMillis() < oldValue.toMillis();
        boolean evenCycleCount = actualCycleProperty.get() % 2 == 0;
        if ((timeline.isAutoReverse() && !evenCycleCount && !smaller)
                || ((evenCycleCount || !timeline.isAutoReverse()) && smaller)) {
            actualCycleProperty.set(actualCycleProperty.get() + 1);
        }
    });

    actualCycleProperty.addListener((observable, oldValue, newValue) -> {
        System.out.println(newValue);
    });
    timeline.play();
}