So I have 6 panels, all of them used a grid layout. And I put them together using gridbaglayout, here is the design I wanted
Here is (another) alternative nested layout. Panels 3 through 6 will get extra height, the extra width will be divided evenly amongst all 6 panels.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class SixPanelNested {
SixPanelNested() {
JPanel gui = new JPanel(new BorderLayout());
gui.setBorder(new TitledBorder("BorderLayout()"));
JPanel north = new JPanel(new GridLayout(1,0));
north.setBorder(new TitledBorder("GridLayout(1,0)"));
gui.add(north, BorderLayout.NORTH);
for (int ii=1; ii<3; ii++) {
JLabel l = new JLabel("Panel " + ii);
l.setBorder(new LineBorder(Color.BLACK));
north.add(l);
}
JPanel south = new JPanel(new GridLayout(1,0));
south.setBorder(new TitledBorder("GridLayout(1,0)"));
gui.add(south);
for (int ii=3; ii<7; ii++) {
JLabel l = new JLabel("Panel " + ii);
l.setBorder(new LineBorder(Color.BLACK));
south.add(l);
}
JOptionPane.showMessageDialog(null, gui);
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SixPanelNested();
}
});
}
}