简体   繁体   中英

I have a 2D array of JButtons, but it seems only one of the JButton is getting added to my panel

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);
    }
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM