【问题标题】:How I can use the listener "currentTimeProperty" ? JavaFX我如何使用监听器“currentTimeProperty”? JavaFX
【发布时间】:2015-01-30 15:48:54
【问题描述】:

我想为“pathTransition.currentTimeProperty()”添加一个监听器,当时间为 250 毫秒时显示一个 println()。

我试过了:

pathTransition2.currentTimeProperty().addListener(new ChangeListener<Duration>() {

                @Override
                public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
                    if(newValue==Duration.millis(250))
                        System.out.println("250ms");
                }
                });

但是什么都没发生……

我做错了什么?

PD:这是full code

【问题讨论】:

  • Oldvalue = 0 和 newValue = 1000,newvalue 永远不会和 250 一样,你的应用卡在一个循环中。
  • 感谢您的回答@Xbit,您知道一种方法来查看何时转换为 250 毫秒吗?

标签: java time properties javafx listener


【解决方案1】:

首先,您的示例没有显示问题,“完整代码”显示了问题。您在 while 循环中一遍又一遍地添加侦听器。

除此之外,您无法检查 newValue==Duration.millis(250) 因为如果您输出 newValue 变量,您会得到例如:

341.6666666666667 ms
378.5 ms
411.8333333333333 ms
441.1666666666667 ms
473.0 ms
487.1666666666667 ms
494.8333333333333 ms
505.1666666666667 ms
521.1666666666666 ms
537.1666666666666 ms

您需要检查 250 毫秒左右的增量。

这是您修改后的代码。您遇到的问题是 currentTimeProperty 达到 1000 时先上升然后下降,达到 0 时上升等等。

这是一个修改后的代码示例:

import static javafx.animation.Animation.INDEFINITE;
import javafx.animation.PathTransition;
import javafx.animation.PathTransitionBuilder;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.PathBuilder;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @author Jose
 */
public class Listener extends Application {

    long prevTime = System.currentTimeMillis();

    @Override
    public void start(Stage primaryStage) {

        Circle circle = new Circle(50);

        Path path2 = PathBuilder.create().elements(new MoveTo(0, 0), new LineTo(0, 80)).build();
        path2.setFill(Color.RED);
        path2.setStroke(Color.RED);
        path2.setStrokeWidth(1);
        path2.setLayoutX(0);
        path2.setLayoutY(0);

        PathTransition pathTransition2 = PathTransitionBuilder.create().duration(javafx.util.Duration.millis(1000)).cycleCount(INDEFINITE).autoReverse(true).path(path2).node(circle).build();

        pathTransition2.currentTimeProperty().addListener(new ChangeListener<Duration>() {

            @Override
            public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {

                long currTime = System.currentTimeMillis();
                if (Double.compare((currTime - prevTime), 250) > 0) {
                    System.out.println("delta: " + (currTime - prevTime) + ", old value: " + oldValue + ", new value: " + newValue);
                    prevTime = currTime;
                }

            }
        });

        pathTransition2.play();

        StackPane root = new StackPane();
        root.getChildren().addAll(path2, circle);

        Scene scene = new Scene(root, 300, 250);

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

    }

    public static void main(String[] args) {
        launch(args);
    }

}

作为值,您将得到如下内容:

delta: 251, old value: 174.33333333333334 ms, new value: 190.33333333333334 ms
delta: 256, old value: 430.3333333333333 ms, new value: 446.1666666666667 ms
delta: 256, old value: 686.3333333333334 ms, new value: 702.3333333333334 ms
delta: 256, old value: 942.3333333333334 ms, new value: 958.3333333333334 ms
delta: 256, old value: 801.6666666666666 ms, new value: 785.6666666666666 ms
delta: 256, old value: 545.6666666666666 ms, new value: 529.6666666666666 ms
delta: 256, old value: 289.6666666666667 ms, new value: 273.6666666666667 ms
delta: 256, old value: 33.666666666666664 ms, new value: 17.666666666666668 ms
delta: 256, old value: 222.33333333333334 ms, new value: 238.33333333333334 ms
delta: 272, old value: 478.3333333333333 ms, new value: 510.3333333333333 ms
delta: 256, old value: 750.3333333333334 ms, new value: 766.3333333333334 ms
delta: 256, old value: 993.6666666666666 ms, new value: 977.6666666666666 ms
delta: 256, old value: 753.5 ms, new value: 721.6666666666666 ms
delta: 256, old value: 481.6666666666667 ms, new value: 465.6666666666667 ms
delta: 256, old value: 225.66666666666666 ms, new value: 209.66666666666666 ms
delta: 256, old value: 30.5 ms, new value: 46.5 ms
delta: 256, old value: 286.5 ms, new value: 302.3333333333333 ms
delta: 256, old value: 542.5 ms, new value: 558.5 ms
delta: 256, old value: 798.5 ms, new value: 814.3333333333334 ms
delta: 256, old value: 945.5 ms, new value: 929.6666666666666 ms
delta: 256, old value: 689.6666666666666 ms, new value: 673.5 ms
delta: 256, old value: 433.5 ms, new value: 417.5 ms
delta: 256, old value: 177.5 ms, new value: 161.5 ms

【讨论】:

  • 很高兴再次见到你@Roland!没用过Delta,可以举个例子吗?
  • 当然,我编辑了帖子。请注意,我从不使用侦听器报告的值。你打算用这个实现的是完全不同的事情;-)
  • 谢谢!!我的朋友@Roland 回答的另一个问题哈哈
【解决方案2】:

您可以创建一个BooleanBinding 并观察:

Duration quarterSecond = Duration.millis(250);
BooleanBinding afterQuarterSecond = Bindings.createBooleanBinding(() -> 
    pathTransition2.getCurrentTime().greaterThanOrEqualTo(quarterSecond), 
    pathTransition2.currentTimeProperty());
afterQuarterSecond.addListener((obs, wasAfter, isNowAfter) -> {
    if (isNowAfter) {
        System.out.println("250 ms...");
    }
});

【讨论】:

  • 感谢您的回答!很好,我会试试你的代码:)
猜你喜欢
  • 2015-11-01
  • 1970-01-01
  • 2016-05-08
  • 2018-10-28
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 2016-10-12
  • 2013-11-29
相关资源
最近更新 更多