my JButtons won't show up
Hello,
Please have a look at this screen capture of a JFrame I'm programming:
http://www.shahspace.com/mazemania.jpg
You'll notice that there are no buttons on the right hand side. This is a problem. I'm trying to create a JPanel to the right of the maze you see in the middle with two buttons in it, and nothing's showing up.
Here's the code for the JFrame (I have added /* */ comments for your convenience - i.e. at the relevant lines of code):
Code:
package framecomponents;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Dimension;
import java.awt.Container;
public class MazeManiaFrame extends JFrame {
// panels
private GamePanel gamePanel = null;
private EastPanel eastPanel = null;
private SouthPanel southPanel = null;
/************************** CONSTRUCTOR **************************/
public MazeManiaFrame() {
// setup frame properties
setTitle("Maze Mania");
setSize(800, 800);
setLocation(200, 50);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = getContentPane();
container.setLayout(new GridBagLayout());
// create gamePanel
gamePanel = new GamePanel(); /* maze you see in the middle */
gamePanel.setBackground(Color.WHITE);
// create JPanel on each side of GamePanel
JPanel NWPanel = new JPanel();
JPanel NorthPanel = new JPanel();
JPanel NEPanel = new JPanel();
eastPanel = new EastPanel(); /* creation of EastPanel */
JPanel SEPanel = new JPanel();
southPanel = new SouthPanel(); /* creation of SouthPanel */
JPanel SWPanel = new JPanel();
JPanel WestPanel = new JPanel();
/* East and South panels set their own preferred size in their constructors */
// set their preferred size
NWPanel.setPreferredSize(new Dimension(50, 50));
NorthPanel.setPreferredSize(new Dimension(400, 50));
NEPanel.setPreferredSize(new Dimension(50, 50));
SEPanel.setPreferredSize(new Dimension(50, 50));
SWPanel.setPreferredSize(new Dimension(50, 50));
WestPanel.setPreferredSize(new Dimension(50, 400));
// add panels to main container with constraints
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
container.add(NWPanel, constraints);
constraints.gridx = 1;
container.add(NorthPanel, constraints);
constraints.gridx = 2;
container.add(NEPanel, constraints);
constraints.gridy = 1;
container.add(eastPanel, constraints); /* add eastPanel */
constraints.gridy = 2;
container.add(SEPanel, constraints);
constraints.gridx = 1;
container.add(southPanel, constraints); /* add southPanel */
constraints.gridx = 0;
container.add(SWPanel, constraints);
constraints.gridy = 1;
container.add(WestPanel, constraints);
constraints.gridx = 1;
container.add(gamePanel, constraints);
pack();
}
/************************** END CONSTRUCTOR **************************/
/************************** ACCESSORS **************************/
/*** GET ***/
// get panels
public GamePanel getGamePanel() {return gamePanel;}
public SouthPanel getSouthPanel() {return southPanel;}
public EastPanel getEastPanel() {return eastPanel;}
// get panel components
public ClockLabel getClockLabel() {return southPanel.getClockLabel();}
/************************** END ACCESSORS **************************/
}
And here's the code for EastPanel:
Code:
package framecomponents;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
public class EastPanel extends JPanel {
public EastPanel() {
setLayout(new GridBagLayout());
setPreferredSize(new Dimension(200, 400));
JButton button1 = new JButton("button 1");
button1.setPreferredSize(new Dimension(150, 30));
JButton button2 = new JButton("button 2");
button2.setPreferredSize(new Dimension(150, 30));
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
add(button1, constraints);
constraints.gridy = 1;
add(button2, constraints);
}
}
The strange thing is I programmed EastPanel exactly as I did SouthPanel, yet southPanel shows up (with the clock below the maze), but the eastPanel doesn't (at least the buttons aren't showing).
Actually, there is one subtle difference between EastPanel and SouthPanel. SouthPanel has these lines of code:
Code:
constraints.weightx = 1;
constraints.anchor = GridBagConstraints.LINE_START;
before adding the clock JLabel. But I tried this. I tried doing this before adding the buttons, and it didn't make a difference.
Can anyone see what I'm doing wrong?