JavaFX 如何以编程方式更改 GridPane 行高
JavaFX How to change the GridPane row height programmatically
我有一个问题让我抓狂了几天。
我有一个 GridPane,我想在单击按钮时隐藏第一行。
这是 FXML 文件
<VBox prefHeight="200.0" prefWidth="100.0">
<children>
<Button fx:id="buttonTest" mnemonicParsing="false" onAction="#handleButtonTestAction" text="Button" />
<GridPane fx:id="gridPaneTest" gridLinesVisible="true" layoutX="0.5" layoutY="0.5" BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label fx:id="labelTopLeft" text="top left">
<font>
<Font size="15.0" />
</font>
</Label>
<Label fx:id="labelTopRight" text="top right" GridPane.columnIndex="1">
<font>
<Font size="15.0" />
</font>
</Label>
<Label text="center left" GridPane.rowIndex="1">
<font>
<Font size="15.0" />
</font>
</Label>
<Label text="center right" GridPane.columnIndex="1" GridPane.rowIndex="1">
<font>
<Font size="15.0" />
</font>
</Label>
<Label text="bottom left" GridPane.rowIndex="2">
<font>
<Font size="15.0" />
</font>
</Label>
<Label text="bottom right" GridPane.columnIndex="1" GridPane.rowIndex="2">
<font>
<Font size="15.0" />
</font>
</Label>
</children>
</GridPane>
</children>
</VBox>
如果我点击按钮,我会这样做
@FXML
public void handleButtonTestAction() {
labelTopLeft.setVisible(false);
labelTopRight.setVisible(false);
gridPaneTest.getRowConstraints().get(0).setMinHeight(0);
gridPaneTest.getRowConstraints().get(0).setPrefHeight(0);
gridPaneTest.getRowConstraints().get(0).setMaxHeight(0);
}
单击按钮后,标签按预期不可见,但第一行的高度根本没有改变。
更改行约束后我必须更新 GridPane 还是有其他事情要做?
谢谢!
Node.setVisible()
只是切换 Node
.
的可见性状态
要从其父级布局计算中排除 Node
,您还必须通过调用 Node.setManaged(false)
.
来设置其托管状态
我有一个问题让我抓狂了几天。
我有一个 GridPane,我想在单击按钮时隐藏第一行。
这是 FXML 文件
<VBox prefHeight="200.0" prefWidth="100.0">
<children>
<Button fx:id="buttonTest" mnemonicParsing="false" onAction="#handleButtonTestAction" text="Button" />
<GridPane fx:id="gridPaneTest" gridLinesVisible="true" layoutX="0.5" layoutY="0.5" BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label fx:id="labelTopLeft" text="top left">
<font>
<Font size="15.0" />
</font>
</Label>
<Label fx:id="labelTopRight" text="top right" GridPane.columnIndex="1">
<font>
<Font size="15.0" />
</font>
</Label>
<Label text="center left" GridPane.rowIndex="1">
<font>
<Font size="15.0" />
</font>
</Label>
<Label text="center right" GridPane.columnIndex="1" GridPane.rowIndex="1">
<font>
<Font size="15.0" />
</font>
</Label>
<Label text="bottom left" GridPane.rowIndex="2">
<font>
<Font size="15.0" />
</font>
</Label>
<Label text="bottom right" GridPane.columnIndex="1" GridPane.rowIndex="2">
<font>
<Font size="15.0" />
</font>
</Label>
</children>
</GridPane>
</children>
</VBox>
如果我点击按钮,我会这样做
@FXML
public void handleButtonTestAction() {
labelTopLeft.setVisible(false);
labelTopRight.setVisible(false);
gridPaneTest.getRowConstraints().get(0).setMinHeight(0);
gridPaneTest.getRowConstraints().get(0).setPrefHeight(0);
gridPaneTest.getRowConstraints().get(0).setMaxHeight(0);
}
单击按钮后,标签按预期不可见,但第一行的高度根本没有改变。 更改行约束后我必须更新 GridPane 还是有其他事情要做?
谢谢!
Node.setVisible()
只是切换 Node
.
要从其父级布局计算中排除 Node
,您还必须通过调用 Node.setManaged(false)
.