【问题标题】:How to maintain GridPane's fixed-size after adding elemnts dynamically动态添加元素后如何保持GridPane固定大小
【发布时间】:2014-10-13 17:24:30
【问题描述】:

我需要创建可以动态更改的棋盘游戏。
它的尺寸可以是 5x5、6x6、7x7 或 8x8。

我将 JavaFX 与 NetBeans 和用于 GUI 的场景构建器结合使用。

当用户选择大于 5x5 的板尺寸时,会发生以下情况:

这是动态添加单元格之前场景构建器上的模板:

对于 GridPane 中的每个单元格,我添加 StackPane + 单元格编号标签:

@FXML
GridPane boardGame;

public void CreateBoard()
    {
       int boardSize = m_Engine.GetBoard().GetBoardSize();
       int num = boardSize * boardSize;
       int maxColumns = m_Engine.GetNumOfCols();
       int maxRows = m_Engine.GetNumOfRows();

       for(int row = 0; row < maxRows ; row++)
       {
             for(int col = maxColumns - 1; col >= 0 ; col--)
             {
                StackPane stackPane = new StackPane();
                stackPane.setPrefSize(150.0, 200.0);
                stackPane.getChildren().add(new Label(String.valueOf(num)));
                boardGame.add(stackPane, col, row);
                num--;
            }
       }
       boardGame.setGridLinesVisible(true);
       boardGame.autosize();
    }

问题是 GridPane 上的堆栈窗格的大小越来越小。
我尝试将它们设置为相同的最小和最大尺寸,但这并没有帮助它们仍然变得更小。

我在网上搜索,但并没有真正找到与我相同的问题。
唯一与我类似的问题在这里找到:
Dynamically add elements to a fixed-size GridPane in JavaFX

但他的建议是使用 TilePane,而我需要使用 GridPane,因为这是一款棋盘游戏,当我需要执行诸如获取行 = 1 和列 = 2 上的单元格等任务时,使用 GridPane 更容易.

编辑:
我从 FXML 中删除了 GridPane 并在控制器上手动创建了它,但现在它打印了一个空白板:

@FXML
GridPane boardGame;
public void CreateBoard()
    {
       int boardSize = m_Engine.GetBoard().GetBoardSize();
       int num = boardSize * boardSize;
       int maxColumns = m_Engine.GetNumOfCols();
       int maxRows = m_Engine.GetNumOfRows();

       boardGame = new GridPane();
       boardGame.setAlignment(Pos.CENTER);
       Collection<StackPane> stackPanes = new ArrayList<StackPane>();
       for(int row = 0; row < maxRows ; row++)
       {
             for(int col = maxColumns - 1; col >= 0 ; col--)
             {
                StackPane stackPane = new StackPane();
                stackPane.setPrefSize(150.0, 200.0);
                stackPane.getChildren().add(new Label(String.valueOf(num)));
                boardGame.add(stackPane, col, row);

                stackPanes.add(stackPane);
                num--;
            }
       }
       this.buildGridPane(boardSize);
       boardGame.setGridLinesVisible(true);
       boardGame.autosize();

       boardGamePane.getChildren().addAll(stackPanes);
    }

    public void buildGridPane(int i_NumOfRowsAndColumns)
    {
        RowConstraints rowConstraint;
        ColumnConstraints columnConstraint;

        for(int index = 0 ; index < i_NumOfRowsAndColumns; index++)
        {
            rowConstraint = new RowConstraints(3, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, VPos.CENTER, true);
            boardGame.getRowConstraints().add(rowConstraint);
            columnConstraint = new ColumnConstraints(3, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, HPos.CENTER, true);
            boardGame.getColumnConstraints().add(columnConstraint);
        }
    }

【问题讨论】:

  • 尝试使用 stackPane.setMaxSize(150.0, 200.0); 冻结 stackpane 的大小AND stackPane.setMinSize(150.0, 200.0);
  • @Uluk Bily,我试过了,但没有帮助
  • 尝试设置标签大小而不是stackpane

标签: javafx-2 javafx-8 fxml


【解决方案1】:

用 cmets 中的解释稍微更改了您的代码。 HTH。

GridPane boardGame;
public void CreateBoard()
{
   int boardSize = m_Engine.GetBoard().GetBoardSize();
   int num = boardSize * boardSize;
   int maxColumns = m_Engine.GetNumOfCols();
   int maxRows = m_Engine.GetNumOfRows();

   boardGame = new GridPane();
   boardGame.setAlignment(Pos.CENTER);
   Collection<StackPane> stackPanes = new ArrayList<StackPane>();
   for(int row = 0; row < maxRows ; row++)
   {
         for(int col = maxColumns - 1; col >= 0 ; col--)
         {
            StackPane stackPane = new StackPane();

            // To occupy fixed space set the max and min size of
            // stackpanes. 
            // stackPane.setPrefSize(150.0, 200.0);
            stackPane.setMaxSize(100.0, 100.0);
            stackPane.setMinSize(100.0, 100.0);

            stackPane.getChildren().add(new Label(String.valueOf(num)));
            boardGame.add(stackPane, col, row);

            stackPanes.add(stackPane);
            num--;
        }
   }
   // No need to add column and row constraints if you want just a uniform 
   // rigid grid view. So commented the line below.

   // this.buildGridPane(boardSize);
   boardGame.setGridLinesVisible(true);
   boardGame.autosize();

   // Here you are adding all stackpanes, those are added to the gridpane 'boardGame' 
   // before, to the another gridpane with name 'boardGamePane'. So all stackpanes are moved
   // to this second gridpane. This is the reason of blank board you are seeing.
   // So commenting this out also.

   // boardGamePane.getChildren().addAll(stackPanes);
}

【讨论】:

  • 谢谢。我标记了你的答案,虽然它不是很完整。除了你的代码我添加到函数的末尾: boardGamePane.getChildren().add(boardGame);现在它工作正常。我只需要将它加载到当前的主窗格中。
猜你喜欢
  • 2014-06-09
  • 2015-12-23
  • 2014-04-13
  • 2018-04-12
  • 2015-04-14
  • 2018-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多