如果我直接在 Scene
元素中使用 JavaFX 元素(例如,Circle
),它会正确定位。但是,如果我直接将其放在 Pane
中(例如下面的 BorderPane
),那么它的位置就不正确。
示例 1(将圆正确定位在 (10,10)):
Circle c = new Circle(10,10,15);
Group g = new Group(c);
Scene s = new Scene(g,800,600);
stage.setScene(s);
stage.show();
示例 2(不起作用):
Circle c = new Circle(10,10,15);
Group g = new Group(c);
BorderPane b = new BorderPane();
b.setCenter(g);
Scene s = new Scene(b,800,600);
stage.setScene(s);
stage.show();
有什么想法吗?
最佳答案
有些 Pane 会处理其子 Pane 的位置,无论您将其 x
和 y
设置为何,而有些则不会。如果您想设置位置,您可能需要在 BorderPane
内放置一个 Pane
。
来自 Pane 的 API 文档:
This class may be used directly in cases where absolute positioning of children is required since it does not perform layout beyond resizing resizable children to their preferred sizes. It is the application's responsibility to position the children since the pane leaves the positions alone during layout.
更多信息请参见:http://docs.oracle.com/javafx/2.0/api/javafx/scene/layout/package-summary.html
关于java - 如何在任何 Pane 中定位 JavaFX 形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9501797/