Java 设置静态属性的动画?

Java 设置静态属性的动画?,java,javafx,javafx-8,Java,Javafx,Javafx 8,我刚刚开始学习JavaFX,非常喜欢它,但我不太确定如何设置AnchorPane.leftAnchor属性的动画 目前,我正在使用一个时间线来设置Node.translateProperty()的动画,这给了我一个很好的平滑动画。然而,尽管我的节点似乎在移动,但就接收鼠标单击而言,它似乎没有移动。实际上,我想做的是为节点的AnchorPane.leftAnchor静态属性设置动画,这可能吗 理想情况下,我希望保留我的时间表,因为它也在做其他事情,所以 1) 是否可以像设置常规属性的动画一样设置静

我刚刚开始学习JavaFX,非常喜欢它,但我不太确定如何设置AnchorPane.leftAnchor属性的动画

目前,我正在使用一个时间线来设置Node.translateProperty()的动画,这给了我一个很好的平滑动画。然而,尽管我的节点似乎在移动,但就接收鼠标单击而言,它似乎没有移动。实际上,我想做的是为节点的AnchorPane.leftAnchor静态属性设置动画,这可能吗

理想情况下,我希望保留我的时间表,因为它也在做其他事情,所以

1) 是否可以像设置常规属性的动画一样设置静态属性的动画,如下所示:

new KeyFrame(Duration.ZERO, new KeyValue(node.translateXProperty(), from, Interpolator.EASE_BOTH))
2) 如果这是不可能的,我可以翻译鼠标点击匹配到翻译的位置吗

干杯


Garry

如果设置
translateX
translateY
属性的动画,则通过
MouseEvent.getX()
MouseEvent.getY()
检索已设置动画的节点上的鼠标坐标,将位于该节点的坐标系的局部:即,它们不会受到节点自身位置变化的影响。如果设置
layoutX
layoutY
属性的动画(如果设置
AnchorPane.leftAnchor
属性的动画,则会执行此操作),则情况也是如此

可以通过调用将节点的局部坐标系中的点转换为其父坐标系

请注意,
节点上有其他类似的方法,如
localToScene
localToScreen
,以及
MouseEvent
上的一些方便方法:
getSceneX()
getScreenX()
,对于
y
也有类似的方法

为了回答您的实际问题(尽管我怀疑这是多余的,也许…),您可以创建一个新属性并“动画化”该属性。然后向其添加一个侦听器,并在其更改时更新
leftAnchor
。这个SSCCE演示了这项技术(尽管您可以使用a代替,并以完全相同的方式获取鼠标坐标)


谢谢你,詹姆斯,你回答的第二部分,创建一个新的财产正是我需要的解决方案,它工作得非常完美。谢谢
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Region;
import javafx.stage.Stage;
import javafx.util.Duration;

public class AnimateXAnchor extends Application {

    @Override
    public void start(Stage primaryStage) {
        Region rect = new Region();
        rect.setPrefSize(100,  100);
        rect.setStyle("-fx-background-color: cornflowerblue;");

        rect.setOnMousePressed(e -> {
            double x = e.getX();
            double y = e.getY();
            System.out.printf("Mouse click in local coordinates: [%.1f, %.1f]%n", x, y);

            Point2D clickInParent = rect.localToParent(x, y);
            System.out.printf("Mouse click in parent coordinates: [%.1f, %.1f]%n%n", clickInParent.getX(), clickInParent.getY());

        });

        AnchorPane root = new AnchorPane();
        root.getChildren().add(rect);
        AnchorPane.setTopAnchor(rect, 10.0);

        DoubleProperty x = new SimpleDoubleProperty();
        x.addListener((obs, oldX, newX) -> AnchorPane.setLeftAnchor(rect, newX.doubleValue()));

        Timeline animation = new Timeline(
            new KeyFrame(Duration.ZERO, new KeyValue(x, 0)),
            new KeyFrame(Duration.seconds(5), new KeyValue(x, 400))
        );

        Scene scene = new Scene(root, 600, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
        animation.play();
    }

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