何时使用平移和何时重定位-平移和布局坐标之间有什么区别?

问题描述:

何时使用平移以及何时为了移动节点而重定位?归根结底,他们似乎在视觉上做同样的事情.移动节点;第一种是通过对原点进行平移(x,y保持不变),第二种是通过更改x,y坐标. 因此,假设我想在屏幕上的特定位置移动节点..我应该使用node.relocate(x,y)还是node.setTranslateX(x),node.setTranslateY(y)?

When to use translate and when relocate in order to move a node? In the end of the day it seems they do the same thing (visually); move the node; the first by doing a translation on the origin (the x, y stays the same), the second by changing the x, y coords. So suppose i want to move a node in a specific point in the screen.. should i use node.relocate(x,y) or node.setTranslateX(x), node.setTranslateY(y)?

为了说明我的意思,我制作了一个可以玩的示例程序: 屏幕上的矩形,其位置由4个滑块(其中2个控制布局x,y,另两个控制平移x,y)确定.

To demostrate what I mean I have made a sample program you can play with: A rectangle on the screen, whose position is determined by 4 sliders (2 of them controlling the layout x, y the other two controlling the translate x, y).

/* imports are missing */  
public class TransReloc extends Application{
    @Override
    public void start(Stage primaryStage) throws Exception {
        Group root = new Group();
        Rectangle rect = new Rectangle(100, 50, Color.BLUE);
        root.getChildren().add(rect);
        VBox controlGroup = new VBox();
        Slider relocX = new Slider(-100, 100, 0 );
        Slider relocY = new Slider(-100, 100, 0 );
        Slider transX = new Slider(-100, 100, 0 );
        Slider transY = new Slider(-100, 100, 0 );
        rect.layoutXProperty().bind(relocX.valueProperty());
        rect.layoutYProperty().bind(relocY.valueProperty());
        rect.translateXProperty().bind(transX.valueProperty());
        rect.translateYProperty().bind(transY.valueProperty());
        controlGroup.getChildren().addAll(relocX, relocY, transX, transY);
        root.getChildren().add(controlGroup);
        controlGroup.relocate(0, 300);
        Scene scene  = new Scene(root, 300, 400, Color.ALICEBLUE);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

layout坐标由诸如StackPane或VBox之类的布局管理器用来控制其子位置.组(和窗格)将子级布局留给开发人员,因此与translate功能没有区别.

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.

因此通常,您只应更改translate坐标以进行精细的位置调整,而将layout留给布局管理器(实际上,您不能更改非Group/Pane布局管理器中的节点的layoutX,layoutY)

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)

作为示例,请尝试运行下一个代码并调整窗口大小,以查看StackPane如何重新计算layoutBounds

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

}