0
public game()
{
    setTitle("game");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);
    setLayout(new BorderLayout());
    buildMenu();
    setJMenuBar(menuBar);
    buildGreetingsPanel();
    add(greetingsPanel, BorderLayout.NORTH);
    buildGamePanel();
    add(gamePanel, BorderLayout.CENTER);
    buildStatusPanel();
    add(statusPanel, BorderLayout.SOUTH);
    buildSettingsPanel();
    add(settingsPanel);
    pack();
    setVisible(true);
}

[...............]

private void buildGamePanel()
{
    gamePanel = new JPanel();
    gamePanel.setBorder(BorderFactory.createLineBorder(difficultyColor));
    gamePanel.setPreferredSize(new Dimension(9 * span, 9 * span));
    gamePanel.setLayout(new GridLayout(40, 40, 0, 0));

    for(col = 0; col < gameWidth; col++)
    {
        for(row = 0; row < gameHeight; row++)
        {
            buttons[col][row] = new JButton();
            buttons[col][row].setBounds(4, 4, span, span);
            buttons[col][row].addMouseListener(new mouseListener());
            gamePanel.add(buttons[col][row]);
        }
    }
}

As the title reads.
I added buildGamePanel(); and add(gamePanel, BorderLayout.CENTER); already. What's the problem? Thanks

Edit: Since nobody answered yet, the problem probably lies somewhere else. I added the public game(); constructor as the possible source of the error.

Andrew Kor
  • 154
  • 5
  • 16
  • What's the error are you getting? – Naveen Jul 27 '13 at 04:50
  • No error at all. The buttons just don't show up. – Andrew Kor Jul 27 '13 at 05:12
  • As far as I understood, flaw lies in this line ` buttons[col][row].setBounds(4, 4, span, span);`, since every value used as an argument of this method remains constant, throughout the life cycle of the loop. Hence all. Buttons will come on the same spot, one on top of another. – nIcE cOw Jul 27 '13 at 10:22
  • Can you post the whole source? I suspect an issue with your layout and as nicE cOw pointed out, you X/Y values for your buttons are all constant at "4". – JBuenoJr Jul 27 '13 at 13:58
  • 1
    For reference, here's a complete [example](http://stackoverflow.com/a/7706684/230513). – trashgod Jul 27 '13 at 16:25
  • @AndrewKor : When you are using a Layout for your GUI, then there is no need to use `setBounds(...)`. Since the same is used with `Absolute Positioning`, which is not a good way to create an application. Though, if you wanted to provide space between two instances of `JButton`, then you can use the overloaded constructor, of `GridLayout` as described in this [answer](http://stackoverflow.com/a/17876938/1057230) – nIcE cOw Jul 27 '13 at 16:59

1 Answers1

0

The problem was with BorderLayout();

When it compiled, settingsPanel was added to the center of the GUI before gamePanel. Forgot to specify it should be placed as follows:

    add(settingsPanel, BorderLayout.EAST);
Andrew Kor
  • 154
  • 5
  • 16