Results 1 to 5 of 5
Thread: my JButtons won't show up
- 09-24-2010, 08:42 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
my JButtons won't show up
Hello,
Please have a look at this screen capture of a JFrame I'm programming:

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):
And here's the code for EastPanel:Java 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 **************************/ }
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).Java 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); } }
Actually, there is one subtle difference between EastPanel and SouthPanel. SouthPanel has these lines of code:
before adding the clock JLabel. But I tried this. I tried doing this before adding the buttons, and it didn't make a difference.Java Code:constraints.weightx = 1; constraints.anchor = GridBagConstraints.LINE_START;
Can anyone see what I'm doing wrong?
-
This is the ideal situation for your creating and posting an SSCCE. Do this and you'll likely get a helpful answer quickly. Much luck!
- 09-25-2010, 02:16 AM #3
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
Okay, I attached a SSCCE. However, I was not able to reproduce the problem. The buttons show up fine in the SSCCE. So I still don't know where the problem lies.
-
Last edited by Fubarable; 09-25-2010 at 03:23 AM.
- 09-25-2010, 04:09 AM #5
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
No need. I solved the problem.
I erased all the class files in the framecomponents package folder (where MazeManiaFrame, EastPanel, SouthPanel, and a few other frame related files are) and tried to recompile.
Usually, when I compile I type "javac *.java" and it works.
This time is didn't for some reason. No class files appeared in the framecomponents folder (even though compiling reported no errors).
I had to go "javac framecomponents/*.java".
Why would this be?
Not only this, but it seems like any change I now make to a file in framecomponents requires the same thing when compiling: javac framecomponents/*.java.
I just recently split my files up into packages and folders for those packages. Is there something more complicated I need to know about compiling with packages?
Similar Threads
-
how to show web browser in mobile app or is it possible to show it in textArea
By Basit781 in forum CLDC and MIDPReplies: 3Last Post: 05-27-2010, 10:54 AM -
Help with JButtons...
By ashton in forum New To JavaReplies: 8Last Post: 01-26-2009, 09:38 AM -
JButtons
By jadaleus in forum Advanced JavaReplies: 4Last Post: 10-17-2008, 02:49 AM -
netbeans 6.0 not show commpunent or show blank page
By fahimaamir in forum NetBeansReplies: 1Last Post: 01-26-2008, 06:20 AM -
JButtons
By fgasimzade in forum SWT / JFaceReplies: 1Last Post: 12-25-2007, 05:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks