GridBagLayout比较复杂,要具体问题具体分析,你不妨多用一点PANEL和简单的布局器

解决方案 »

  1.   

    这个样子的成吗?import java.awt.*;
    import javax.swing.*;public class Test extends JFrame {
      JPanel p;
      JButton b1, b2, b3, b4, b5, b6, b7, b8;
      GridBagLayout gb;
      GridBagConstraints gbc;  public Test() {
        super("MyLayout Test");
        gb = new GridBagLayout();
        gbc = new GridBagConstraints();
        p = new JPanel();
        p.setLayout(gb);
        getContentPane().add(p);    b1 = new JButton("1");
        b2 = new JButton("2");
        b3 = new JButton("3");
        b4 = new JButton("4");
        b5 = new JButton("5");
        b6 = new JButton("6");
        b7 = new JButton("7");
        b8 = new JButton("8");    gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(1,1,1,1);    gbc.gridy = 1;
        gbc.gridwidth = 1;
        p.add(b1,gbc);
        gbc.gridwidth = 2;
        p.add(new JLabel(""),gbc);
        gbc.gridwidth = 1;
        p.add(b2,gbc);
        gbc.gridy++;
        p.add(new JLabel(""),gbc);
        p.add(b3,gbc);
        p.add(b4,gbc);
        gbc.gridy++;
        p.add(new JLabel(""),gbc);
        p.add(b5,gbc);
        p.add(b6,gbc);
        gbc.gridy++;
        p.add(b7,gbc);
        gbc.gridwidth = 2;
        p.add(new JLabel(""),gbc);
        gbc.gridwidth = 1;
        p.add(b8,gbc);    /*gbc.anchor = GridBagConstraints.NORTHWEST;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gb.setConstraints(b1, gbc);
        p.add(b1);    gbc.gridx = 1;
        gbc.gridy = 1;
        gb.setConstraints(b2, gbc);
        p.add(b2);    gbc.gridx = 0;
        gbc.gridy = 2;
        gb.setConstraints(b3, gbc);
        p.add(b3);    gbc.gridx = 1;
        gbc.gridy = 2;
        gb.setConstraints(b4, gbc);
        p.add(b4);//********************************************
        gbc.anchor = GridBagConstraints.NORTHEAST;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gb.setConstraints(b5, gbc);
        p.add(b5);    gbc.gridx = 0;
        gbc.gridy = 2;
        gb.setConstraints(b6, gbc);
        p.add(b6);    gbc.gridx = 1;
        gbc.gridy = 1;
        gb.setConstraints(b7, gbc);
        p.add(b7);    gbc.gridx = 1;
        gbc.gridy = 2;
        gb.setConstraints(b8, gbc);
        p.add(b8);*/    pack();
        setSize(400, 400);
        setVisible(true);
        setDefaultCloseOperation(Test.EXIT_ON_CLOSE);  }  public static void main(String args[]) {
        new Test();
      }
    }
    有问题了再说