How to manipulate GridLayout settings accordingly

2019-05-14 15:40发布

问题:

I have a setLayout(new GridLayout(5,5,3,3));

5 rows and 5 columns. On the first row I want one big row. You know how in excel, where you would merge cells. So I just want to merge the 5 columns only on the first row. I am putting a JLabel on the first row, and its very annoying that I need to have multiple JLabels for each grid cell and make sure spacing works out well.

Is there a way to do this on GridLayout? Or I am stuck with 5 rows and 5 col strictly?

回答1:

GridBagLayout can do this. Alternatively, you can use nested layouts: add the first row to the NORTH of BorderLayout and remaining GridLayout to the CENTER. See also A Visual Guide to Layout Managers.

Addendum: MiGLayout is also well worth a look in this context. The Web Start demo is particularly appealing, and the manual section on Merging and Splitting Cells may be useful.