翻译转换JavaFX

TranslateTransition JavaFX

我正在尝试对我的矩形执行 TranslateTransition,但它根本没有执行任何操作。 这是我的代码:

primaryStage.setTitle("AnimationTest");
    Group group = new Group();

    Rectangle rect = new Rectangle(0,0,100,100);
    group.getChildren().add(rect);
    TranslateTransition transition =new TranslateTransition(Duration.millis(1000),rect);
        transition.setByX(100);

    Button button = new Button("StartAnimation");
        button.setOnAction((e)->{
            transition.play();
        });
    VBox layout = new VBox();
    layout.getChildren().addAll(group, button);
    Scene scene = new Scene(layout, 600, 600);
    primaryStage.setScene(scene);
    primaryStage.show();

根据 Group 的文档:

A Group will take on the collective bounds of its children

换句话说,当矩形移动时,组的坐标系会调整以适应它包含的所有内容。由于它唯一包含的是矩形,因此矩形相对于组的容器保持固定。

使用 Pane 代替 Group:

primaryStage.setTitle("AnimationTest");
Pane pane = new Pane();

Rectangle rect = new Rectangle(0,0,100,100);
pane.getChildren().add(rect);
TranslateTransition transition =new TranslateTransition(Duration.millis(1000),rect);
    transition.setByX(100);

Button button = new Button("StartAnimation");
    button.setOnAction((e)->{
        transition.play();
    });
VBox layout = new VBox();
layout.getChildren().addAll(pane, button);
Scene scene = new Scene(layout, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();