Java Swing setting JPanel Size

This may not answer your immediate question…but…

GridLayout layout = new GridLayout(1, 2);
this.setLayout(layout);
// You're original code...
// Why are you using `BorderLayout.CENTER` on a `GridLayout`
this.add(new PaintSurface(), BorderLayout.CENTER);

You set the layout as a GridLayout, but you are using BorderLayout constraints to apply one of the components??

Also, make sure that there are not calls to Test#pack else where in your code, as this will override the values of setSize

UPDATED (from changes to question)

Remember, the default layout manager for JFrame is BorderLayout, so even though you’re calling buttonPanel.setSize, it’s likely that it’s begin overridden by the layout manager anyway.

I would take a read through A Visual Guide to Layout Managers and Using Layout Managers to find a layout manager that best meets your requirements.

If you can’t find a single one, consider using compound components with different layout managers to bring the layout closer to what you want to achieve.

Leave a Comment