如何布局按钮,以便填充可用空间

时间:2013-06-06 11:21:16

标签: javafx-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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

我遇到了与ComboBox相同的问题并以这种方式解决了这个问题:

<ComboBox hgrow="ALWAYS" maxWidth="Infinity" />