【问题标题】:JavaFx can't get the node coordinate relative to scene [duplicate]JavaFx无法获取相对于场景的节点坐标[重复]
【发布时间】:2018-03-13 03:10:50
【问题描述】:

我的代码:

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 的坐标。谢谢。

【问题讨论】:

  • System.out.println() 语句移到primaryStage.show() 之后。
  • 大小和位置是在布局过程中分配的,这在您的代码中没有发生。

标签: java javafx javafx-2 javafx-8


【解决方案1】:

我相信你必须得到父边界,然后指定你想要的相对于那些边界。

System.out.println(btn2.getBoundsInParent().getY());
System.out.println(btn2.getBoundsInParent().getX());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    相关资源
    最近更新 更多