Grid bag layout not displaying the way I want

后端 未结 4 1986
暗喜
暗喜 2021-01-06 18:00

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

4条回答
  •  萌比男神i
    2021-01-06 18:19

    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.

    SixPanelNested

    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();
                }
            });
        }
    }
    

提交回复
热议问题