1

I'm trying to make a simple game where you can set out tiles that make up a celtic design and I can't seem to make the buttons stay the same size. Here's the code:

public class CTGContent {

    public static JPanel content = new JPanel();
    private static JButton board[][] = new JButton[20][20];

    static private Random random = new Random();

    public static int CTGcolumn = random.nextInt(14)+1;
    public static int CTGRow = 15;
    private static GridLayout boardLayout;

    public final int getBoardWidth(){
        return CTGcolumn*40;
    }

    public final int getBoardHeight(){
        return CTGRow*40;
    }

    public static void initializeBoard(){
        int row = 0;
        int column = 0;

        int defaultCOL = 0;
        int defaultROW = 0;

        boardLayout = new GridLayout(CTGRow, CTGcolumn);
        content.setLayout(boardLayout);

        Random determine = new Random(); 

        while (row < CTGRow){
            column = 0;
            while (column < CTGcolumn){
                board[row][column] = new JButton(new ImageIcon("images\\template.gif"));
                board[row][column].setPreferredSize(new Dimension(40, 40));
                board[row][column].setMaximumSize(new Dimension(40, 40));
                board[row][column].setMinimumSize(new Dimension(40, 40));
                content.add(board[row][column]);
                column++;
            }
            row++;
        }
    }

    private static void build(){

        initializeBoard();

        JFrame window = new JFrame("testing the menubar");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setContentPane(content);
        content.add(new JLabel("My Label"));
        window.pack();
        window.setVisible(true);    

    }

    public CTGContent() {

    }

    public CTGContent(JFrame window){

        initializeBoard();
        window.add(content);

    }

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater( new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                    build();
            }
        });
    }
}

It starts off the right size usually but then for some reason if I resize the window or something they change size with it. I need them to stay the same size no matter what. How can I do that?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user214936
  • 19
  • 1
  • 7
  • Please use code formatting for code, input/output & structured documents like HTML or XML. To do that, *select the sample and click the `{}` button above the messaged posting/editing form.* – Andrew Thompson Jan 20 '14 at 07:55
  • Note that the linked 'duplicate' is most definitely **not**. The linked question wanted to keep a group of buttons the same size as *each other*, and the answer was `GridLayout`. This question already used `GridLayout` but wanted to keep the buttons the same size *when the window resized* and the answer was put the entire panel into a parent panel that respects the size of the content and won't resize it! – Andrew Thompson Jan 20 '14 at 22:44

2 Answers2

3

Change:

window.setContentPane(content);

To:

JPanel contentCenter = new JPanel(new GridBagLayout());
contentCenter.add(content);
window.setContentPane(contentCenter);

enter image description here

enter image description here

There are other ways to do it, like make the layout a FlowLayout, but the GBL will keep it nicely centered - both vertically and horizontally.

Tips

  1. Please learn common Java naming conventions (specifically the case used for the names) for class, method & attribute names & use them consistently.
  2. For better help sooner, post an MCVE. That code only needed import in order to make it an MCVE.
  3. See also this answer to Centering a JLabel on a JPanel

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

You use GridLayout because of your buttons resize horisontally/vertically always to fill whole grid cell. To prevent that you can use another LayoutManager, for example GridBagLayout.

Always read about setting size to components.

Community
  • 1
  • 1
alex2410
  • 10,904
  • 3
  • 25
  • 41