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