【问题标题】:JavaFX border pane Grid [duplicate]JavaFX边框窗格网​​格[重复]
【发布时间】:2017-06-23 10:25:54
【问题描述】:

我有一个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

【问题讨论】:

  • 为什么不把矩形放到一个数组中?

标签: java javafx grid borderpane


【解决方案1】:

您需要使用静态方法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;
}

【讨论】:

  • 这与使用 for 每个循环遍历 GridPane 不一样吗?
  • 这会导致 NPE:null 可以用来代替 Integer 0
猜你喜欢
  • 2014-01-20
  • 2016-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-17
  • 1970-01-01
  • 2020-07-24
  • 1970-01-01
相关资源
最近更新 更多