0

我有一个BorderPane,它的中心窗格是GridPane. GridPane有 20x20的矩形对象

 for (int rows = 0; rows < GRID_SIZE; ++rows) {
     for (int cols = 0; cols < GRID_SIZE; ++cols) {
            Rectangle r = new Rectangle(RECTANGLE_SIZE, RECTANGLE_SIZE);
            grid.add(r, rows, cols);

grid.add方法接受:节点子级 - r、列和行索引。

如何使用此索引访问网格

BorderPane的班级是静态的

  private static BorderPane bp = new BorderPane();

所以当我输入 bp.getCenter (网格)时,我找不到任何合适的方法来插入列和行索引,这会返回我的Rectangle对象​​?

编辑:解决方案JavaFX:按行和列获取节点

4

1 回答 1

0

您需要使用静态方法GridPane.getColumnIndex(col)GridPane.getRowIndex(n). 试试这个代码:

public Optional<Rectangle> findByIndex(GridPane gridPane, int row, int col) {
    final Optional<Rectangle> rectangle = gridPane.getChildren().stream().map(n -> (Rectangle) n).filter(n -> GridPane.getColumnIndex(n) == col && GridPane.getRowIndex(n) == row).findFirst();
    return rectangle;
}
于 2017-02-06T13:26:22.953 回答