动态添加元素后如何保持GridPane的固定大小 [英] How to maintain GridPane's fixed-size after adding elemnts dynamically

查看:90
本文介绍了动态添加元素后如何保持GridPane的固定大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to create board game that can be dynamically change.
Its size can be 5x5, 6x6, 7x7 or 8x8.

我将JavaFX与NetBeans和GUI的Scene Builder结合使用.

I am jusing JavaFX with NetBeans and Scene builder for the GUI.

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

When the user choose board size greater than 5x5 this is what happens:

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

This is the template on the scene builder before adding cells dynamically:

我要在GridPane中的每个单元格上添加StackPane +单元格编号的标签:

To every cell in the GridPane I am adding StackPane + label of the cell number:

@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上的堆栈窗格的大小越来越小.
我试图将它们的最小和最大大小设置为相等,但这并没有帮助它们变得越来越小.

The problem is the stack panes's size on the GridPane are getting smaller.
I tried to set them equal minimum and maximum size but it didn't help they are still getting smaller.

我在网上搜索,但没有真正发现与我的问题相同的问题.
在这里找到与我唯一类似的问题:
在JavaFX中将元素动态添加到固定大小的GridPane中

I searched on the web but didn't realy find same problem as mine.
The only similar problem to mine was found here:
Dynamically add elements to a fixed-size GridPane in JavaFX

但是他的建议是使用TilePane,而我需要使用GridPane,因为这是一个棋盘游戏,并且当我需要执行诸如在row = 1和column = 2上的单元格之类的任务时,使用GridPane更容易.

But his suggestion is to use TilePane and I need to use GridPane because this is a board game and it more easier to use GridPane when I need to do tasks such as getting to cell on row = 1 and column = 2 for example.


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


I removed the GridPane from the FXML and created it manually on the Controller but now it print a blank board:

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

推荐答案

使用注释中的说明对代码进行了少许更改. HTH.

Changed your code slightly with explanations in comments. 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);
}

这篇关于动态添加元素后如何保持GridPane的固定大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆