TranslateTransition JavaFX

时间:2015-10-18 12:24:55

标签: user-interface animation javafx translate

我正在尝试将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();

1 个答案:

答案 0 :(得分:0)

根据Group的文档:

  

小组将承担其子女的集体界限

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

使用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();