2

我需要在 2x2 网格中布置 4 个按钮。所有按钮必须具有相同的大小,并在窗口改变它的大小时改变它。

我现在使用以下 FXML,但按钮不会改变它们的大小。

<GridPane xmlns:fx="http://javafx.com/fxml">
    <Button fx:id="btnLogin" text="Login" GridPane.rowIndex="0" GridPane.columnIndex="0"/>
    <Button fx:id="btnLibrary" text="Library" GridPane.rowIndex="0" GridPane.columnIndex="1"/>
    <Button fx:id="btnRegister" text="Register" GridPane.rowIndex="1" GridPane.columnIndex="0"/>
    <Button fx:id="btnHelp" text="Help" GridPane.rowIndex="1" GridPane.columnIndex="1"/>
</GridPane>
4

2 回答 2

1

我就是这样设法做到的。

<GridPane xmlns:fx="http://javafx.com/fxml">
    <columnConstraints>
        <ColumnConstraints percentWidth="50"/>
        <ColumnConstraints percentWidth="50"/>
    </columnConstraints>
    <rowConstraints>
        <RowConstraints percentHeight="50"/>
        <RowConstraints percentHeight="50"/>
    </rowConstraints>
    <Button fx:id="btnLogin" text="Login" GridPane.rowIndex="0" GridPane.columnIndex="0" maxWidth="10000" maxHeight="10000"/>
    <Button fx:id="btnLibrary" text="Library" GridPane.rowIndex="0" GridPane.columnIndex="1" maxWidth="10000" maxHeight="10000"/>
    <Button fx:id="btnRegister" text="Register" GridPane.rowIndex="1" GridPane.columnIndex="0" maxWidth="10000" maxHeight="10000"/>
    <Button fx:id="btnHelp" text="Help" GridPane.rowIndex="1" GridPane.columnIndex="1" maxWidth="10000" maxHeight="10000"/>
</GridPane>
于 2013-06-06T11:54:12.387 回答
0

我对 ComboBox 有同样的问题并以这种方式解决了它:

<ComboBox hgrow="ALWAYS" maxWidth="Infinity" />
于 2017-05-04T16:48:04.610 回答