问题描述
我正在使用JavaFX 8创建应用程序.我使用拖放功能动态更改网格窗格的内容.我希望按行或按行/列对GridPane内容进行迭代.JavaFX允许通过指定行和列在GridPane中添加节点.
I am creating an application using JavaFX 8.I change the content of a grid pane dynamically using drag/drop.I wish to iterate GridPane contents per row or per row/col.JavaFX allows adding nodes in a GridPane by specifying the row and column.
gridPane.add(node, col, row);
我想通过指定行和列以相同的方式读取GridPane的节点.
我想要类似以下内容的源代码(以下代码不正确),
I would like to read the nodes of a GridPane on the same way, by specifying the row and column.
I would like to have something similar to the below source (the below code is not correct),
for(int row = 0; row < gridPaneHeight; row++) {
for(int col = 0; row < gridPaneWidth; col++) {
Node node = gridPane.get(col, row);
}
}
推荐答案
如何
int[][] gridPaneNodes = new int[gridPaneWidth][gridPaneHeight] ;
for (Node child : gridPane.getChildren()) {
Integer column = GridPane.getColumnIndex(child);
Integer row = GridPane.getRowIndex(child);
if (column != null && row != null) {
gridPaneNodes[column][row] = child ;
}
}
(或者,当您将它们放置在哪个单元格中时,您只需跟踪它们放置在哪个单元格中即可...)
(or, you could just keep track of which were placed in which cell when you put them there...)
那你就可以做
for (int row = 0; row < gridPaneHeight; row++) {
for(int col = 0; row < gridPaneWidth; col++) {
Node node = gridPaneNodes[column][row] ;
}
}
这篇关于JavaFX-每行迭代GridPane节点-每行读取GridPane的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!