Reputation: 1
So I'm working an an assignment and I seemed to be stuck on a bug I can't seem to overcome. Essentially when trying to create a 10x10 grid, it isn't appearing while everything else (eg. title and menu) is when I'm running the application. Here's the essential part of what I've done so far
public class minesweeperGUI extends JFrame{
private JFrame gameGUI;
private JPanel board;
private JButton[][] boardButtons = new JButton [10][10];
public minesweeperGUI(){
gameGUI = new JFrame("Enhanced Minesweeper"); //Title of the Window
gameGUI.setVisible(true); //We want (true) to set it visible
gameGUI.setSize(400,550); //400 550
gameGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //This will close the frame when you press X. IMPORTANT
gameGUI.getContentPane().setBackground(Color.CYAN);
gameGUI.setLayout(null);
//Panel for minesweeper board
board = new JPanel();
GridLayout experimentLayout = new GridLayout(10, 10);
board.setLayout(experimentLayout);
board.setSize(400,550);
for(int x=0;x<boardButtons.length;x++){
for(int y=0;y<boardButtons[x].length;y++){
boardButtons[x][y] = new JButton();
board.add(boardButtons[x][y]);
}
}
gameGUI.add(board);
}
public static void main(String[] args) {
minesweeperGUI mainInterface = new minesweeperGUI();
}
}
Any type of help would be greatly appreciated :)
Upvotes: 0
Views: 94
Reputation: 109815
gameGUI.setVisible(true); //We want (true) to set it visible
minesweeperGUI()
, otherwise you added JComponent
s to the already visible Swing Gui (missing notifier for automatical refresh, repaint)minesweeperGUI mainInterface = new minesweeperGUI();
should be wrapped into invokeLater
, more in Oracle trail Initial Thread
class name should be MinesweeperGUI
, search for Java Namings Convention
gameGUI.setLayout(null); and board.setSize(400,550);
missing there setBounds
for board.setSize(400,550);
(placing JPanel
to JFrame
, because JPanel
has zero Dimension
)
there no reason to use null layout
, remove gameGUI.setLayout(null);
override getPreferredSize
for private JPanel board;
remove code lines gameGUI.setLayout(null);
and board.setSize(400,550);
add code line pack()
before setVisible(true)
, for proper sizing for JFrame
(based on getPreferredSize
for private JPanel board;
)
Upvotes: 2
Reputation: 57381
Remove the gameGUI.setLayout(null);
call and move the next line to be after buttons adding
gameGUI.setSize(400,550); //400 550
gameGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //This will close the frame when you press X. IMPORTANT
gameGUI.getContentPane().setBackground(Color.CYAN);
gameGUI.setVisible(true); //We want (true) to set it visible
Upvotes: 1
Reputation: 3454
I think one Problem is that you mix up your instances...
public static void main(String[] args) {
minesweeperGUI mainInterface = new minesweeperGUI();
}
makes a new Instance of your minesweeperGUI wich extends JFrame - so this simply makes a new JFrame...
but in your construktor you create another JFrame instance private JFrame gameGUI;
and gameGUI = new JFrame("Enhanced Minesweeper");
...
i suggest you remove the private variable gameGUI (private JFrame gameGUI;
) and use simply this
for your layout...
//private JFrame gameGUI; //remove this one!
private JPanel board;
private JButton[][] boardButtons = new JButton [10][10];
public minesweeperGUI(){
super("Enhanced Minesweeper"); //construktor from superclass
this.setVisible(true); //use of this
this.setSize(400,550); //use of this
...
//Panel for minesweeper board
board = new JPanel();
GridLayout experimentLayout = new GridLayout(10, 10);
board.setLayout(experimentLayout);
board.setSize(400,550);
for(int x=0;x<boardButtons.length;x++){
for(int y=0;y<boardButtons[x].length;y++){
boardButtons[x][y] = new JButton();
board.add(boardButtons[x][y]);
}
}
this.add(board); //use of this
}
you don't HAVE to write this.setXXX()
you can also simply write setVisible(true)
for instance...
Upvotes: 0