简体   繁体   中英

Gridbag layout not aligning properly

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.

I believe I found the issue in the code.

In the class pClassico.java , try to change the line 296 :

interior.add(MontonesJuego[i], "cell " + (i+4) + " 3");

with this one :

interior.add(MontonesJuego[i], "cell " + (i+5) + " 3");

Here is what I have on my environment : Solitarios与对齐的桩。

Hope it will help !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM