1

I am a beginner. I am programming a Java game, and I am having trouble changing the size of the button buttonPlayAgain. The size of the button does not change using the code below. How can I change the size of the button?
Here is my code:

 public hoppa(IModele modele) {

    super(new GridLayout(1, 1));
    setSize(VueGrille.FACT * modele.getGrille().getLongueur(), 1);

    Dimension dim = new Dimension(1, 1);

    labMines = new JLabel();
    labMines.setPreferredSize(dim);
    add(labMines);

     buttonPlayAgain = new JButton("New Game");
    //buttonPlayAgain.setSize(1, 1);
    buttonPlayAgain.setPreferredSize(new Dimension(50,50));
    add(buttonPlayAgain);

    labTimer = new JLabel();
    labTimer.setPreferredSize(dim);
    add(labTimer);

    initValues(modele);

}
ASCIIThenANSI
  • 865
  • 1
  • 9
  • 27

2 Answers2

0

It can be normal that your button is not resized while calling setPreferredSize. try setSize instead. See Java: Difference between the setPreferredSize() and setSize() methods in components

Community
  • 1
  • 1
JFPicard
  • 5,029
  • 3
  • 19
  • 43
0

Use setSize() if your component's parent has no layout manager, and setPreferredSize() (see also setMinimumSize and setMaximumSize) if it does.

setSize() most likely won't do anything if the component's parent is using a layout manager

Fundhor
  • 3,369
  • 1
  • 25
  • 47