如何在动态插入GridPane后自动调整主窗格的大小

时间:2014-08-22 19:31:31

标签: java java-ee javafx scenebuilder

我有AnchorPane,它是包含所有其他窗格的Main 在它内部我有板区域AnchorPane和骰子区域也是AnchorPane。 这是它在场景构建器上的外观: enter image description here

在Board区域内,这是AnchorPane我正在动态创建(根据用户请求)板的大小(5x5,6x6,7x7或8x8):

  GridPane boardGame;

    @FXML
    Button dice;

    @FXML
    AnchorPane boardArea;

    @FXML
    AnchorPane boardGameAnchorPane;

    @FXML
    AnchorPane dicePane;

 public void CreateBoard()
    {
       int boardSize = m_Engine.GetBoard().GetBoardSize();
       int num = 1;
       int maxColumns = m_Engine.GetNumOfCols();
       int maxRows = m_Engine.GetNumOfRows();
       boardGame = new GridPane();
       //boardGame.setAlignment(Pos.CENTER);

       for(int row = maxRows - 1; row >= 0 ; row--)
       {
             for(int col =  0; col < maxColumns ; col++)
             {
                StackPane stackPane = new StackPane();
                stackPane.setMaxSize(SIZE_OF_CELL, SIZE_OF_CELL);
                stackPane.setMinSize(SIZE_OF_CELL, SIZE_OF_CELL);

                if((col + row) % 2 != 0)
                {
                     stackPane.getStyleClass().add("oddCellBorder");
                }
                else
                {
                    stackPane.getStyleClass().add("evenCellBorder");
                }

                Label label = new Label(String.valueOf(num));
                StackPane.setAlignment(label, Pos.BOTTOM_LEFT);
                stackPane.getChildren().add(label);

                boardGame.add(stackPane, col, row);
                num++;
            }
       }

       this.fixBoardGameSize();
       boardGame.setGridLinesVisible(true);
      // boardGame.autosize();
       boardArea.getChildren().add(boardGame);

       //ImageView imageView = ImageUtils.getImageView("diceTransprntBack3D.png");
       //dice.setGraphic(imageView);
       // Image img1 = new Image(getClass().getResourceAsStream("about.png"));
    }

我的问题从电路板尺寸7x7或8x8开始 董事会区域扩展到骰子区域:
enter image description here

我尝试使用此功能手动修复尺寸:

private void fixBoardGameSize()
{
    boardArea.setMinHeight(boardGame.getHeight());
    boardArea.setMaxHeight(boardGame.getHeight());
    boardArea.setMinWidth(boardGame.getWidth());
    boardArea.setMaxWidth(boardGame.getWidth());

   boardGameAnchorPane.setPrefWidth(boardArea.getWidth() + dicePane.getWidth() + 500 );
   boardGameAnchorPane.setMinWidth(boardArea.getWidth() + dicePane.getWidth() + 500 );
   boardGameAnchorPane.setMaxWidth(boardArea.getWidth() + dicePane.getWidth() + 500 );
   dicePane.setLayoutX(boardArea.getLayoutX() + boardArea.getWidth() + 1000);
}

但没有成功。
我也尝试使用锚定疼痛约束但没有成功 我搜索但找不到可以帮助我的东西所以我在这里问,如果有人知道如何保持两个锚窗格分开。

1 个答案:

答案 0 :(得分:0)

根据您的描述,使用相同的锚定窗格,并给定固定大小的应用程序,您可以做的是:

首先,设置boardArea的最大大小,在该窗格上正确显示网格的边距

// Maximum fixed size you give to boardArea
public static final double MAX_SIZE = 600;

// Margin to border
public static final double MARGIN = 25;

现在,在您的fixBoardGameSize()方法中,正确调整网格大小:

private void fixBoardGameSize(){
    // Listener, since boardGame dimensions are determined after the stage is shown
    ChangeListener<Number> resize = (ov, v, v1) -> {
        double scale = Math.min((MAX_SIZE - 2d*MARGIN) / boardGame.getWidth(), 
                                (MAX_SIZE - 2d*MARGIN) / boardGame.getHeight());
        boardGame.setScaleX(scale);
        boardGame.setScaleY(scale);
        boardGame.setTranslateX((MAX_SIZE - boardGame.getWidth()) / 2d);
        boardGame.setTranslateY((MAX_SIZE - boardGame.getHeight()) / 2d);
    };

    boardGame.widthProperty().addListener(resize);
    boardGame.heightProperty().addListener(resize);
}

现在,您可以显示包含任意数量的行和列的游戏。它总是适合电路板,并且会正确居中。