gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:13:51 25 4
gpt4 key购买 nike

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

为了演示我的意思,我制作了一个示例程序,您可以使用:屏幕上的一个矩形,其位置由 4 个 slider 确定(其中 2 个控制布局 x、y,另外两个控制平移 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);
}
}

最佳答案

StackPane 或 VBox 等布局管理器使用

layout 坐标来控制其子级位置。组(和 Pane )将子布局留给开发人员,因此与翻译功能没有区别。

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

作为示例,尝试运行下一个代码并调整窗口大小以查看 StackPane 如何重新计算 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();

}

关于javafx-2 - 何时使用平移以及何时重新定位 - 平移坐标和布局坐标之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28835920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com