Issue with adding JPanel objects with layout managers
Author |
Message |
Kam
![](http://compsci.ca/v3/uploads/user_avatars/14687339244b17231868552.png)
|
Posted: Sat Mar 06, 2010 5:17 am Post subject: Issue with adding JPanel objects with layout managers |
|
|
Hey I've posted this on a few other forums but haven't gotten much replies.. so I'll try here. Anyways...
The games I made in the past did not contain any customized menus (ie. not from JOptionPane) different components dividing the display screen... so I thought I would try to on my current game I'm working on. So here's the issue: I'm trying to set the GUI up for the game screen.. but when I try to add in additional JPanel objects into the my main JFrame object, all of the JPanel objects stack up at the very top left corner (their not supposed to because I am using a layout manager). Here's my simplified version of the code:
code: |
public void setUpGame(){
MainGame game = new MainGame();
game.setLayout(new BoxLayout(game, BoxLayout.Y_AXIS) );
playPanel = new PlayPanel(); // don't worry about this.. it's a subclass of JPanel.
playPanel.setPreferredSize( new Dimension(playPanel.width, playPanel.height) );
game.add(playPanel);
JPanel collections = new JPanel();
collections.setLayout(new FlowLayout() );
collections.setPreferredSize( new Dimension( 1280, 256));
miniMapPanel = new MiniMapPanel(); // subclass of JPanel.
miniMapPanel.setPreferredSize( new Dimension(miniMapPanel.width, miniMapPanel.height) );
collections.add(miniMapPanel);
statsPanel = new StatsPanel();
statsPanel.setPreferredSize( new Dimension(statsPanel.width , statsPanel.height ) );
collections.add(statsPanel);
... // I do the same thing for StatsPanel and CommandPanel objects
game.add(collections);
add(game);
// referencing to the JFrame object
this.pack();
setVisible(true);
setFocusable(true);
}
|
Notes:
-The sum of miniMapPanel.width, statsPanel.width, ... , commandPanel.width add up to 1280 pixels.
- miniMapPanel.height, statsPanel.height, ... , commandPanel.height all have the same height of 256 pixels.
- What I'm trying to do is set up a GUI very similar to that of StarCraft, and WarCraft.
- The game is set up in full screen.
When I compile the program, I see (and I've tested for it) that all of the JPanel objects that were created are all stacked one on top of each other, in the top left corner of the screen. Any ideas and/or suggestions are welcome. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
facultyofmusic
![](http://compsci.ca/v3/uploads/user_avatars/15137545114a8c72a6dc7e5.gif)
|
Posted: Fri Mar 12, 2010 5:50 pm Post subject: Re: Issue with adding JPanel objects with layout managers |
|
|
consider using the null layout? It's a lot better than using other complicated ones unless you need the elements to be dynamically resizable... |
|
|
|
|
![](images/spacer.gif) |
|
|