如何从 GridPane 获取 ImageView(不是 Node)

How to Get ImageView (not Node) from GridPane

我必须从 GridPane 获取 ImageView。

我使用此代码,但它适用于 Node,而我正在尝试更改 ImageView(在 GridPane 内)中的图像。

private Node getNodeFromGridPane(GridPane gridPane, int col, int row) {
      for (Node node : gridPane.getChildren()) {
         if (GridPane.getColumnIndex(node) == col && GridPane.getRowIndex(node) == row) {
            System.out.println(node);
            return node;
         }
      }
      return null;
   }

有解决办法吗?

我已经搜索了很多帖子,但都没有解决这个问题。 谢谢

在来电方,您可以执行以下操作:

final Node foundNode = getNodeFromGridPane(gridPane, col, row);
if (foundNode instanceof ImageView) {
    //do something
}

instanceof 将同时处理 ImageView 和 null。