如何动态添加元素后维护GridPane的固定大小

时间:2014-08-20 11:13:28

标签: javafx-2 javafx-8 fxml

我需要创建可以动态变化的棋盘游戏 它的大小可以是5x5,6x6,7x7或8x8。

我正在将JavaFX与用于GUI的NetBeans和场景构建器混为一谈。

当用户选择大于5x5的电路板尺寸时,会发生这种情况:
enter image description here

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

对于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,因为这是一个棋盘游戏,当我需要执行任务时更容易使用GridPane,例如在row = 1和column = 2上获取单元格

修改
我从FXML中删除了GridPane并在Controller上手动创建了它,但现在它打印出一块空白板:

@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);
        }
    }

1 个答案:

答案 0 :(得分:1)

使用评论中的说明稍微更改了您的代码。 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);
}