Search code examples
javabuttonresizejframeresizable

I want my JFrame to adjust its dimensions and keep the buttons with constant size


i need to keep my buttons with constant size and re-size the JFrame to adjust to the buttons size, but all i get is a constant size on the JFrame and my buttons re-size according to the amount of buttons.

Here is my code.

Here is where i create my buttons, the number of buttons is chosen by the dimensions received

[campoMinado.getAltura()][campoMinado.getLargura()]

      Height                Width

botoes[][] is my bi-dimensional array of buttons.

for(int y = 0; y < campoMinado.getAltura(); y++){
    for(int x = 0; x < campoMinado.getLargura(); x++){
        botoes[x][y] = new BotaoCampoMinado(x, y);
        botoes[x][y].setPreferredSize(new Dimension(40,40));
        botoes[x][y].setMinimumSize(new Dimension(40,40));
        botoes[x][y].setMaximumSize(new Dimension(40,40));
        botoes[x][y].addActionListener(action);
        botoes[x][y].addMouseListener(mouseListener);
        getContentPane().add(botoes[x][y]);

     }
 }

Solution

  • When creating the JFrame use .pack();

    Ex:

    JFrame gameField = new JFrame();
    gameField.pack();
    gameField.setVisible(true);