我想要一个网格面板布局,如附图所示。任何人都知道该怎么做还是我使用了错误的布局?
最佳答案
您可以使用列和行约束来控制 GridPane 中不同行和列的增长方式。
这样做的方法是将第一列的 hgrow
属性设置为 ALWAYS,并将第一行的 vgrow
属性设置为 ALWAYS - 这意味着顶部左侧单元格将始终展开。您可以将其他单元格设置为固定的宽度/高度,否则它们将展开以适应其内容,而不会更多。
我已经包含了一个 FXML 示例,GridPane 位于 AnchorPane 内,并锚定在 AnchorPane 的顶部、左下角和右侧。这意味着 GridPane 将增长以填充父 AnchorPane:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
<children>
<GridPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="ALWAYS" />
<ColumnConstraints prefWidth="150" />
</columnConstraints>
<rowConstraints>
<RowConstraints vgrow="ALWAYS" />
<RowConstraints prefHeight="150" />
</rowConstraints>
</GridPane>
</children>
</AnchorPane>
这会产生此处所示的布局,并展开以填充父 Pane :
关于resize - 网格调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14390261/