我的代码:
Button btn1 = new Button("Button");
Button btn2 = new Button("The second Button");
VBox vbox = new VBox();
vbox.getChildren().addAll(btn1,btn2);
Scene scene = new Scene(vbox, 300, 300);
System.out.println(btn2.getBoundsInLocal());
System.out.println(btn2.getBoundsInParent());
System.out.println(btn2.getLayoutBounds());
System.out.println(btn2.layoutBoundsProperty());
打印:
BoundingBox [minX:0.0, minY:0.0, minZ:0.0, width:0.0, height:0.0, depth:0.0, maxX:0.0, maxY:0.0, maxZ:0.0]
BoundingBox [minX:0.0, minY:0.0, minZ:0.0, width:0.0, height:0.0, depth:0.0, maxX:0.0, maxY:0.0, maxZ:0.0]
BoundingBox [minX:0.0, minY:0.0, minZ:0.0, width:0.0, height:0.0, depth:0.0, maxX:0.0, maxY:0.0, maxZ:0.0]
ReadOnlyObjectProperty [bean: Button@5f3c42b9[styleClass=button]'The second Button', name: layoutBounds, value: BoundingBox [minX:0.0, minY:0.0, minZ:0.0, width:0.0, height:0.0, depth:0.0, maxX:0.0, maxY:0.0, maxZ:0.0]]
我的问题是:当我得到btn1的坐标时,MinX和MinY为0,我能理解。但是关于btn2,MinY不应该是0;并且MaxX和MaxY也不应该为0.那么如何在VBox中获得按钮左角的实际坐标。要清楚,我需要相对于VBox的节点的坐标。感谢。
答案 0 :(得分:0)
我相信你必须获得父界限然后指定你想要的那些。
System.out.println(btn2.getBoundsInParent().getY());
System.out.println(btn2.getBoundsInParent().getX());