Reputation: 3
I recently started learning Java and aam writing a gui for a character creation sheet for personal use. I believe i have gotten all of the back end stuff working properly, but I can't test it outside of the console because I can't get the GUI to display anything other than a gray box.
I have a main class that creates the JFrame and calls the constructors for my other character classes to do all of the work, but for some reason, nothing shows up in the GUI. I'm not getting any errors or anything and I have spent several hours researching possible solutions to no avail.
My code: The main method:
public static void main(String[] args) throws IOException
{
JFrame main = new JFrame();
JMenuBar menu = new MenuBar();
JPanel character = new CreatorGUI();
main.getContentPane().add(character);
main.add(menu);
main.setJMenuBar(menu);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setSize(10000, 10000);
main.pack();
main.setVisible(true);
}
The CreatorGUI:
public CreatorGUI() throws IOException
{
JPanel container = new JPanel();
JPanel stats = makeStats();
JPanel mods = makeMods();
JPanel character = makeChar();
container.add(mods);
container.add(stats);
container.add(character);
add(container);
}
I am trying to get the GUI working in its most basic form to make sure everything is working before I go back and refine the GUI, but I have hit a wall.
Any help is much appreciated!
Upvotes: 0
Views: 36
Reputation: 324207
main.getContentPane().add(character);
main.add(menu);
main.setJMenuBar(menu);
Using main.getContentPane().add(...)
is the same as main.add(...)
. That is the component gets added to the content pane.
The default layout manager of a JFrame is a BorderLayout.
When you don't specify a constraint the component is added to the CENTER
. However only a single component can be added to the CENTER
so the "menu" is replacing the "character" panel.
Get rid of the main.add(menu)
statement. It should only be added to the menubar so you only need to use the setJMenuBar(...)
method.
Upvotes: 2
Reputation: 1
The most suspect method call there is setSize which takes the pixel height and width. 10000 is probably too large as a width and a height and so you may only be seeing a portion of what you're trying to display. Changing it to something like 800 x 800 may allow you to see new things.
Upvotes: 0