在JavaFX中居中对齐网格窗格的行
我在fxml中有一个GridPane,它有一个文本标题和4个按钮。GridPane本身是居中对齐的,但所有按钮都在网格的列内左对齐。我知道如何使用Java代码更改项目的对齐方式,但这不是理想的情况,因为我希望所有样式都使用FXML和CSS处理。有人能建议在JavaFX的GridPane视图中居中对齐单元格元素的最佳方法吗?在FXML中:在JavaFX中居中对齐网格窗格的行,java,javafx,gridpane,Java,Javafx,Gridpane,我在fxml中有一个GridPane,它有一个文本标题和4个按钮。GridPane本身是居中对齐的,但所有按钮都在网格的列内左对齐。我知道如何使用Java代码更改项目的对齐方式,但这不是理想的情况,因为我希望所有样式都使用FXML和CSS处理。有人能建议在JavaFX的GridPane视图中居中对齐单元格元素的最佳方法吗?在FXML中: <GridPane ...> <columnConstraints> <!-- one of these for e
<GridPane ...>
<columnConstraints>
<!-- one of these for each column: you can obviously have other properties set here if needed -->
<ColumnConstraints halignment="CENTER" />
</columnConstraints>
</GridPane>
您需要单独对齐网格窗格中的每个对象,而不是网格窗格本身
要执行此操作,请转到布局:ObjectName(ex.按钮),并在HAlignment处选择“中心”。要将
GridPane
的子节点与中心对齐,需要设置子节点的和
在Java代码中,您可以执行类似的操作:
GridPane.setHalignment(node, HPos.CENTER); // To align horizontally in the cell
GridPane.setValignment(node, VPos.CENTER); // To align vertically in the cell
在FMXL中,您可以通过以下方式实现类似效果:
<GridPane>
... // other properties
<children>
<Button mnemonicParsing="false" text="Button" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
</children>
</GridPane>
同样,您也可以添加行约束
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" halignment="CENTER" minWidth="10.0" prefWidth="100.0" />
... // More constraints for other columns
</columnConstraints>
<children>
<Button mnemonicParsing="false" text="Button" />
</children>
</GridPane>