JavaFX边框窗格

时间:2017-02-06 13:01:19

标签: java javafx grid borderpane

我有BorderPane,其中心窗格为GridPaneGridPane有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: Get Node by row and column

1 个答案:

答案 0 :(得分: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;
}
相关问题