我在AnchorPane上放了一个Grid窗格,我的Grid窗格有一组TextField,标签和按钮,我想把它放进锚窗格,但是我的Gridpane只是粘在Anchorpane的左上方,设置了setTopanchor等等。对于GridPane,但它不适用于锚窗格,我希望它放在中间。这是样品。是否有任何缺失,因为它只是不真正适用于AnchorPane?我会等你的帮助。

发布于 2015-05-31 23:36:46
如果我正确地回答了您的问题,并且您希望将GridPane放在AnchorPane中,您可以在每个AnchorPane宽度或高度变化(anchorPane.widthProperty().addListener())上尝试这样的操作:
double x = (anchorPane.getWidth() - GRID_SIZE) / 2;
double y = (anchorPane.getHeight() - GRID_SIZE) / 2;
AnchorPane.setRightAnchor(grid, x);
AnchorPane.setTopAnchor(grid, y);
AnchorPane.setLeftAnchor(grid, x);
AnchorPane.setBottomAnchor(grid, y);基本上,你需要计算每一边的偏移量,并设置一个锚。
下面是StackPane和AncorPane的一些演示代码:https://github.com/varren/JavaFX-Center-GridPane-in-other-Pane/tree/master/src/sample
但是,如果您只希望网格采用完整的AnchorPane大小,则只需设置
AnchorPane.setRightAnchor(grid, .0);
AnchorPane.setTopAnchor(grid, .0);
AnchorPane.setLeftAnchor(grid, .0);
AnchorPane.setBottomAnchor(grid, .0);而且不需要监听AnchorPane大小的变化。
https://stackoverflow.com/questions/30543382
复制相似问题