我在 java fxml 文件中定义了一个网格窗格,如下所示:
<GridPane fx:id="grid" gridLinesVisible="true" prefHeight="256" prefWidth="256">
...
<children>
<Label maxHeight="1.8" maxWidth="1.8" />
<Label maxHeight="1.8" maxWidth="1.8" GridPane.columnIndex="1" />
<Label maxHeight="1.8" maxWidth="1.8" GridPane.columnIndex="2" />
<Label maxHeight="1.8" maxWidth="1.8" GridPane.rowIndex="1" />
<Label maxHeight="1.8" maxWidth="1.8" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label maxHeight="1.8" maxWidth="1.8" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<Label maxHeight="1.8" maxWidth="1.8" GridPane.rowIndex="2" />
<Label maxHeight="1.8" maxWidth="1.8" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label maxHeight="1.8" maxWidth="1.8" GridPane.columnIndex="2" GridPane.rowIndex="2" />
</children>
...
</GridPane>
网格为 3 x 3,每个单元格中都有一个标签。是否可以遍历网格并更改每个标签的文本,如下面的伪代码所示:
for (cell : grid)
{
cell.label.setText("x");
}