[英]JavaFX StackPane showing incorrect (X,Y) Coordination
我正在嘗試打印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());
}
輸出:
[Rectangle[x=0.0, y=0.0, width=50.0, height=50.0, fill=0x000000ff], Rectangle[x=0.0, y=0.0, width=75.0, height=75.0, fill=0x000000ff]]
從上面的輸出中可以看到。 可以使用高度和寬度,但不顯示X和Y布局/平移。 我也使用過: rec1.setLayoutX(230)
但這並沒有改變任何東西。
問題是什么? 我該如何解決呢? 謝謝。
要打印輸出信息( Node.toSring
中Node.toSring
包含), Node.toSring
根據需要定制打印輸出。 例如:
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);
}
}
如果嘗試rec1.setX(100)
,它將顯示為[Rectangle [x = 100.0,y = 0.0]。 與rec1.setY()
相同。 看看有關setX() , setTranslateX() , setLayoutX()的文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.