本文介绍了JavaFX:按行和列获取节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我知道它的位置(行和列)或从gridPane获取节点的任何其他方式,有没有办法从gridPane获取特定节点?

Is there any way to get a specific Node from a gridPane if I know its location (row and column) or any other way to get a node from a gridPane?

推荐答案

我没有看到任何直接的API来获取逐行列索引,但你可以使用 getChildren API来自窗格 getRowIndex(Node child) getColumnIndex(Node child) from GridPane

I don't see any direct API to get node by row column index, but you can use getChildren API from Pane, and getRowIndex(Node child) and getColumnIndex(Node child) from GridPane

//Gets the list of children of this Parent. 
public ObservableList<Node> getChildren() 
//Returns the child's column index constraint if set
public static java.lang.Integer getColumnIndex(Node child)
//Returns the child's row index constraint if set.
public static java.lang.Integer getRowIndex(Node child)

以下是示例代码使用 GridPane

public Node getNodeByRowColumnIndex (final int row, final int column, GridPane gridPane) {
    Node result = null;
    ObservableList<Node> childrens = gridPane.getChildren();

    for (Node node : childrens) {
        if(gridPane.getRowIndex(node) == row && gridPane.getColumnIndex(node) == column) {
            result = node;
            break;
        }
    }

    return result;
}

重要更新: getRowIndex() getColumnIndex()现在是静态方法,应该更改为 GridPane.getRowIndex(node) GridPane.getColumnIndex(节点)

Important Update: getRowIndex() and getColumnIndex() are now static methods and should be changed to GridPane.getRowIndex(node) and GridPane.getColumnIndex(node).

这篇关于JavaFX:按行和列获取节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 10:18