csga5000
csga5000

Reputation: 4141

Using Layouts in Java

I have a JFrame with layout BorderLayout, I add JPanels using BorderLayout.CENTER I have a JPanel(added using CENTER), that uses GridLayout to place 4 buttons.

These buttons are MASSIVE, taking up the whole screen. Or, I do it my way as follows:

Obviously the second option looks better, but as I wish to conform to the norm, and always use layouts... How to I mix customization and layouts?(Or just solve my problem at all)

Upvotes: 1

Views: 195

Answers (1)

Code-Apprentice
Code-Apprentice

Reputation: 83527

When you add a componeent to BorderLayout.CENTER, it will expand to fill the remaining space of the container not used by the other portions of the BorderLayout. Also, you can only add one component at a time to BorderLayout.CENTER. Subsequent adds will replace the previous component.

As for solving your exact problem, I suggest you start by taking a look at A Visual Guide to Layout Managers. This will give you a primer on what layouts are available in the Swing API. Oracle also has more in-depth tutorials for each layout. By nesting layouts, you can give your UI any look that you wish while leveraging their power, especially auto-calculations when a window is resized or otherwise changed.

Upvotes: 4

Related Questions