gpt4 book ai didi

JavaFX StackPane 显示不正确的 (X,Y) 协调

转载 作者:行者123 更新时间:2023-12-01 19:42:13 26 4
gpt4 key购买 nike

我正在尝试打印 StackPane 中的所有节点。我需要获取节点的 X、Y 坐标以及高度和宽度(所有矩形)。然而,它显示 X,Y 坐标为 (0,0),即使我专门选择了其他坐标。

代码:

@Override
public void start(Stage primaryStage) {
Rectangle rec1 = new Rectangle();
rec1.setTranslateX(230);
rec1.setTranslateY(230);
rec1.setWidth(50);
rec1.setHeight(50);

Rectangle rec2 = new Rectangle();
rec2.setTranslateX(150);
rec2.setTranslateY(150);
rec2.setWidth(75);
rec2.setHeight(75);

StackPane root = new StackPane();
root.getChildren().add(rec1);
root.getChildren().add(rec2);

Scene scene = new Scene(root, 1280, 720);

primaryStage.setTitle("Shapes");
primaryStage.setScene(scene);
primaryStage.show();

System.out.println(root.getChildren());
}

输出:

[矩形[x=0.0,y=0.0,宽度=50.0,高度=50.0,填充=0x000000ff],矩形[x=0.0,y=0.0,宽度=75.0,高度=75.0,填充= 0x000000ff]]

正如您从上面的输出中看到的。高度和宽度有效,但 X 和 Y 布局/平移未显示。我还使用了:rec1.setLayoutX(230)但这并没有改变任何东西。

有什么问题吗?我该如何解决这个问题?谢谢。

最佳答案

要打印 Node.toString 中包含的信息以外的信息,请根据您的需要定制打印输出。例如:

print(root.getChildren());
private void print(ObservableList<Node> children) {
for(Node child: children){
double width = child.getBoundsInLocal().getWidth();
Object height = child.getBoundsInLocal().getHeight();
double x = child.getTranslateX();
double y = child.getTranslateY();
System.out.println("trans x - y: " + x + " - "+y + " width - height: " + width +" - "+ height);
}
}

关于JavaFX StackPane 显示不正确的 (X,Y) 协调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54959089/

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