Basically, how do I generate this? I'm pretty sure this is a job for GridBagLayout
, but I'm unable to wrap my head around how to properly size the 'Action Pane' versus the 'Menubar'. The red & black lines indicate the grid that I believe you are to use in this case (3x3), but I might be completely wrong and there could be a way to do it in a different configuration. I tried to mess around with the weightx
, weighty
, gridheight
, gridwidth
values in GridBagConstraints
, but I can't achieve my goals here.
Note that the second red line is supposed to be exactly one-third of the height of the bottom half of the frame.
This is my latest attempt, by trying to use a 3x6 grid (c is the GridBagConstraints object, characterPortraits contain all the portraits, and currentScreen is the 'Action Pane') :
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.25;
c.weighty = (1/6);
c.gridx = 0;
c.gridy = 0;
c.gridheight = 3;
pane.add(characterPortraits.get(0), c);
c.gridx = 2;
pane.add(characterPortraits.get(1), c);
c.gridx = 0;
c.gridy = 3;
c.gridheight = 3;
pane.add(characterPortraits.get(2), c);
c.gridx = 2;
pane.add(characterPortraits.get(3), c);
//c.fill = GridBagConstraints.NONE;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 1;
c.gridy = 0;
c.gridheight = 3;
pane.add(currentScreen, c);
Instead, this produces each portrait in the bottom third of its quadrant, and the Action Pane taking 5/6 of the center column instead of 4/6, like I want it to. Any ideas would help; thanks! -B.
EDIT: I'm designing this application to have a fixed window size; people may say this is bad design, but I'm really just trying to get a feel for Swing components and make sure they at least behave in a fixed window the way I want them to. I think I may allow for a maximize proper-resizing, but that's about it.
Though, it appeared strange to me that you adding your
JMenuBar
, at the most weird of positions. Though what you can do, to overcome the difficulty mentioned by you in this lineis to add one
JPanel
at this location and put yourActionPane
andJmenuBar
to this addedJPanel
, to achieve the desired outcome. So I had addedcenterPanel
at this location to demonstrate, how this can be achieved.I hope this output is what you so desired :
And here is the code responsible for this output :
I've never been a big fan of GridBagLayout. It's hard to get right - each component can have up to 11 constraints - it's hard to maintain, and reusing the same constraints object can lead to unintended consequences.
Instead, I prefer to nest various types of layout. To me, you clearly have:
BorderLayout
, withWest
,Center
, andEast
panelsWest
panel, you have aGridLayout
- 2 rows, 1 column (Portraits 1 and 2).East
panel, you have aGridLayout
- 2 rows, 1 column (Portraits 3 and 4).Center
panel, you have anotherBorderLayout
, with aCenter
component (Action Pane) and aSouth
component (Menubar).The only limitation to this is that the West panel and East panel are not logically connected. If Portraits 1, 2, 3, and 4 are all of the same size, that's not a problem. If they're of different sizes, you may find that the West and East panels will be shaped differently.