I'm working on a solitaire game with java and swing, and I'm experiencing an anoying issue. I have a panel for the game with a miglayout that seems to work fine, but inside of it there are 7 panels more with the piles of the cards, each of them has a gridbaglayout but as you can see in the next image, the piles aren't aligned correctly:
The main panel is grey and the panels with the cards are in green.
The code were the subpanels are created is here: https://github.com/ccokee/Solitarios/blob/master/src/pClasico.java#L166
And the code of the sub panels is here: https://github.com/ccokee/Solitarios/blob/master/src/pMontonC.java#L33
Can someone help me to solve this? I've been messing around with this for a long run but no success :(
By default the GridBagLayout
will center the components vertically (and horizontally) in the panel unless one of the components has a weighty
value not equal to 0.
This will give the extra space of the panel to that component. Read the section from the Swing tutorial on How to Use GridBagLayout for more information.
However, I would suggest a better solution is to use a vertical BoxLayout and then you can just add "glue" to the end to take up the extra space.
Something like:
Box box = Box.createVerticalBox();
box.add( card1 );
box.add( card2 );
...
box.add( Box.createVerticalGlue() );
Another option might be to use the Overlap Layout. This layout manager allows you to position components on top of one another.