0

I'm having trouble getting my GUI to contain any of my JButtons or JTextField. I have two classes One "SnowballFight" class that contains my main method and frame constructor. Then I also have "GameBoard" which sets up my GUI. My problem is that my GUI appears, but appears empty.

"SnowballFight" class:

package snowballfight;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SnowballFight extends JFrame implements ActionListener{
    public SnowballFight(){
        setLayout(new BorderLayout(1,2));
    }
    public static void main(String[] args) {
        SnowballFight frame = new SnowballFight();
        GameBoard game = new GameBoard(frame);
    }
    public void actionPerformed(ActionEvent event) {
    }
}

"GameBoard" class:

package snowballfight;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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){
        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);
    }
    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) {
    }
}
  • 2
    Side note: unless you have a good reason, don't extend `JFrame`. Just have a `JFrame` field in your class. Favour composition over inheritance. – Duncan Jones Nov 28 '13 at 17:06

2 Answers2

1

You never make the GameBoard JFrame visible

game.setVisible(true);

Some notes:

Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • What? `game` is a `JButton[][]`. *You never make the GameBoard JFrame visible* actually, he did: `frame.setVisible(true);` – Josh M Nov 28 '13 at 17:07
  • In `SnowballFight` game is the class that extends JFrame, ofc this class could be eliminated completely and no he only makes `frame` visible which contains _nothing_ `game` is the frame that contains the components :) – Reimeus Nov 28 '13 at 17:17
1

Not sure why there are two frames, but I think you're adding display and panel to the wrong frame.

Try changing:

    add(display, BorderLayout.NORTH);
    add(panel, BorderLayout.SOUTH);

to:

    frame.add(display, BorderLayout.NORTH);
    frame.add(panel, BorderLayout.SOUTH);
tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • You fixed it! Thank you so much! What do you mean 2 frames? I have the snowballFight frame and the GameBoard constructor takes that frame to add more to it. – Digital Sauce Nov 28 '13 at 17:11
  • @DigitalSauce you are welcome! There is no need for `GameBoard` to extend `JFrame`. Keep one frame. no need for two. Looks like `SnowballFight` is not needed at all. – tenorsax Nov 28 '13 at 17:13
  • I'll be adding a lot more functionality to snowballFight later, but i understand now why gameboard doesn't have to extend JFrame. Thanks again for all your help. – Digital Sauce Nov 28 '13 at 17:18