【发布时间】:2020-10-23 20:35:11
【问题描述】:
我想要一个 HBox 容器,其中包含 3 个宽度均匀的按钮,但是当父级的宽度不能分成整数部分时,其中一个节点会更少。如果我的 HBox 是 245px,我有 3 个按钮,其中 1 个是 81px,其他是 82px。
问题是在 HBox 顶部我有一个加载圆圈指示器,圆圈位于 HBox 的中心,当中间按钮未居中时,加载圆圈在 HBox 顶部看起来也未居中。
HBox root = new HBox();
root.setFillHeight(true);
for (int i = 0; i < 3; i++) {
AnchorPane pane = new AnchorPane();
HBox.setHgrow(pane, Priority.ALWAYS);
pane.setMaxHeight(Double.MAX_VALUE);
root.getChildren().add(pane);
}
Scene scene = new Scene(root, 245, 50, Color.TRANSPARENT);
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.show();
root.getChildren().forEach(node -> {
AnchorPane pane = ((AnchorPane)node);
System.out.println(pane.getWidth());
});
想法是它是一个登录场景,提交用户名和密码后,服务器正在加载,hbox顶部有一个圆圈ProgressIndicator,圆圈居中。
中间按钮上方的圆圈看起来没有居中。那么如何在不明确设置按钮宽度的情况下使用布局容器来做到这一点。布局是否总是在整数上分配孩子?
【问题讨论】: