0

I'm trying to get my JPanel to have 100 Jbuttons in a 10 by 10 grid, but when I run my code only 1 large button is appearing. I posted my code bellow all imports have been removed to save space all necessary imports are in the project, but not shown here.

Here is my "SnowballFight" class:

package snowballfight;
public class SnowballFight extends JFrame implements ActionListener{
    private JTextField display = new JTextField("Welcome to the SnowBall fight!");
    /**
     * @param args the command line arguments
     */
    public SnowballFight(){
        setLayout(new BorderLayout(1,2));
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout( 10, 10));
        display.setHorizontalAlignment(JButton.CENTER);
        display.setEditable(false);
        add(display, BorderLayout.NORTH);
        add(panel, BorderLayout.SOUTH);
    }
    public static void main(String[] args) {
        SnowballFight frame = new SnowballFight();
        GameBoard game = new GameBoard(frame);
    }
    public void actionPerformed(ActionEvent event) {
    }
}

and my "GameBoard" class:

package snowballfight;
public class GameBoard extends JFrame implements ActionListener{
    private JButton[][] game = new JButton[10][10];
    private JTextField display = new JTextField("Welcome to the SnowBall fight!");
    private Opponent[] opponents = new Opponent[4];


    public GameBoard(SnowballFight frame){
        for (int row = 0; row < game.length; row++) {
            for (int col = 0; col < game[row].length; col++) {
                game[row][col] = new JButton();
                game[row][col].setBackground(Color.gray);
                game[row][col].addActionListener(this);
                frame.add(game[row][col]);
            }
        }
        frame.setTitle("Snowball Fight");
        frame.setSize(200, 150);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public boolean isGameOver(){
        for (int opponent = 0; opponent < opponents.length; opponent++) {
            if(opponents[opponent].isSoaked() == false ){
                return false;
            }
        }
        return true;
    }
    public void actionPerformed(ActionEvent event) {
    }
}

New GameBoard class:

public GameBoard(SnowballFight frame){
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout( 10, 10));
        for (int row = 0; row < game.length; row++) {
            for (int col = 0; col < game[row].length; col++) {
                game[row][col] = new JButton();
                game[row][col].setBackground(Color.gray);
                game[row][col].addActionListener(this);
                panel.add(game[row][col]);
            }
        }
        add(display, BorderLayout.NORTH);
        add(panel, BorderLayout.SOUTH);
        frame.setTitle("Snowball Fight");
        frame.setSize(200, 150);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
  • *"to save space all necessary imports are in the project."* 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Nov 28 '13 at 05:38

1 Answers1

3
frame.add(game[row][col]);

You do realize the default layout for JFrame is BorderLayout right? And that only one component can appear in any one area of a BorderLayout? Use a GridLayout instead.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    It's not just the default layout, it's explicitly set by the OP ;) – MadProgrammer Nov 28 '13 at 05:41
  • Line 27 and 28 of snowballFight class I created a JPanel and added grid layout to that panel. I'm trying to add them to that grid am I not? – Digital Sauce Nov 28 '13 at 05:43
  • Both statements are true, the BorderLayout is the default and it was reset to a BorderLayout. – camickr Nov 28 '13 at 05:44
  • @user2771155 BorderLayout was applied to the frame, GridLayout was applied to a JPanel, which was added to the frame, but, the buttons were added to the frame....instead, move "panel" to the GameBoard constructor and add your buttons to it, then add it to the frame – MadProgrammer Nov 28 '13 at 05:46
  • Alright I'm adding the panel to my Gameboard class instead then adding the buttons to the panel. If I am understand everyone this should fix my issue. – Digital Sauce Nov 28 '13 at 05:49
  • I have changed my GameBoard class to below and now my window displays nothing. (edited my post to have new code.) – Digital Sauce Nov 28 '13 at 05:54
  • *"(edited my post to have new code.)"* Again, and a little louder this time.. *For better help sooner, post one [SSCCE](http://sscce.org/) (as opposed to **several uncompilable code snippets).*** – Andrew Thompson Nov 28 '13 at 06:58