Question:
I have a JFrame with five buttons added to the contents panel. I intended for the X axis to begin at the very top of the frame, however, when the frame is resized the contents are shifted towards the middle. How do I maintain all of the components floating to the top.How it looks
Here is my code (shortened)
Answer:
GridBagLayout
will, by default, layout components around the middle of the container.You can, however, modify the amount of remaining space a component will occupy through the use of
weightx
and weighty
, for example, if you added…
Now, personally, I’d consider using compound layouts, as finding the “last”
y
position might not always be feasibleSee How to Use GridBagLayout for more details
If you have better answer, please add a comment about this, thank you!