JavaFX边框窗格网格

JavaFX边框窗格网格,java,javafx,grid,borderpane,Java,Javafx,Grid,Borderpane,我有一个边框窗格,它的中心窗格是网格窗格。 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,

我有一个
边框窗格
,它的中心窗格是
网格窗格
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);
因此,当我键入bp.getCenter(网格)时,我找不到任何合适的方法来插入列和行索引,这将返回我的
矩形
对象

编辑:
解决方案您需要使用静态方法
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;
}
public可选findByIndex(GridPane GridPane,int row,int col){
最后一个可选矩形=gridPane.getChildren().stream().map(n->(矩形)n).filter(n->gridPane.getColumnIndex(n)==col&&gridPane.getRowIndex(n)==row.findFirst();
返回矩形;
}

为什么不将矩形放入数组中?这不等于在网格窗格上用for each循环进行迭代吗?这会导致NPE:
null
可以用来代替
整数
0
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;
}