When to use translate and when relocate - What is the difference between translate and layout coordinates?

你说的曾经没有我的故事 提交于 2019-11-30 09:11:29

layout coordinates are used by Layout Managers like StackPane or VBox to control their children location. Group (and Pane) leaves children layout to developer thus there is no difference from translate functionality.

So generally you should only change translate coordinates for fine location tuning and leave layout to layout managers (actually you can't change layoutX, layoutY for nodes inside non Group/Pane layout managers)

As an example try to run next code and resize window to see how StackPane recalculates layoutBounds

public void start(Stage primaryStage) throws Exception {
    StackPane root = new StackPane();
    Rectangle rect = new Rectangle(100, 50, Color.BLUE);
    root.getChildren().add(rect);
    Scene scene  = new Scene(root, 300, 300, Color.ALICEBLUE);

    rect.layoutXProperty().addListener( (e) -> {
        System.out.println(rect.getLayoutX() + ":" + rect.getLayoutY());
            });

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

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!