你一直在用一个GridBagConstraints对象-gridBagC
所以你的top、left、right1、right2都是一个对象修改 每次都新new一个    public GridBagLayoutExample() {
        GridBagLayout gridBag = new GridBagLayout() ;
        this.setLayout(gridBag);
        GridBagConstraints gridBagC = new GridBagConstraints() ;
         
        gridBagC.fill = GridBagConstraints.HORIZONTAL ;
        gridBagC.gridx = 0 ;
        gridBagC.gridy = 0 ;
        gridBagC.gridwidth = 2 ;
        gridBag.setConstraints(top, gridBagC);
        this.add(top) ;
         
        gridBagC = new GridBagConstraints() ;
        gridBagC.gridx = 0 ;
        gridBagC.gridy = 1 ;
        gridBagC.gridheight = 2 ;
        gridBagC.fill = GridBagConstraints.VERTICAL ;
        gridBag.setConstraints(left, gridBagC);
        this.add(left) ;
 
        gridBagC = new GridBagConstraints() ;
        gridBagC.gridx = 1 ;
        gridBagC.gridy = 1 ;
        gridBag.setConstraints(right1, gridBagC);
        this.add(right1);
 
        gridBagC = new GridBagConstraints() ;
        gridBagC.gridx = 1 ;
        gridBagC.gridy = 2 ;
        gridBag.setConstraints(right2, gridBagC);
        this.add(right2);
    }

解决方案 »

  1.   

    主要原因是因为你之前更改了height width 但是下一个button的时候 没有把width和height改回来.
    例如你在top的时候
    gridBagC.gridwidth = 2 ;
    但是你在left的时候没有把width改回1
    gridBagC.gridwidth = 1;
    下面是left的时候改的height
    gridBagC.gridheight = 2 ;
    在right1的时候要把height改回1
    gridBagC.gridheight = 1 ;这样就行了.
    不过为了防止这种事情发生
    一般都是分别对每个button新建一个GridBagConstraints的下面是新的code:
    import java.awt.BorderLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.WindowConstants;
       
    @SuppressWarnings("serial")
    public class GridBagLayoutExample extends JPanel {
        private JButton top    = new JButton() ;
        private JButton left   = new JButton() ;
        private JButton right1 = new JButton() ;
        private JButton right2 = new JButton() ;
        public GridBagLayoutExample() {
            GridBagLayout gridBag = new GridBagLayout() ;
            this.setLayout(gridBag);
            GridBagConstraints gridBagC = new GridBagConstraints() ;
             
            gridBagC.fill = GridBagConstraints.HORIZONTAL ;
            gridBagC.gridx = 0 ;
            gridBagC.gridy = 0 ;
            gridBagC.gridwidth = 2 ;
            gridBag.setConstraints(top, gridBagC);
            this.add(top) ;
             
            gridBagC.gridx = 0 ;
            gridBagC.gridy = 1 ;
            gridBagC.gridwidth = 1 ;
            gridBagC.gridheight = 2 ;
            gridBagC.fill = GridBagConstraints.VERTICAL ;
            gridBag.setConstraints(left, gridBagC);
            this.add(left) ;
     
            gridBagC.gridx = 1 ;
            gridBagC.gridy = 1 ;
            gridBagC.gridheight = 1 ;
            gridBag.setConstraints(right1, gridBagC);
            this.add(right1);
     
            gridBagC.gridx = 1 ;
            gridBagC.gridy = 2 ;
            gridBag.setConstraints(right2, gridBagC);
            this.add(right2);
        }
        public static void main(String[] args) {
            JFrame f = new JFrame("GridBagLayoutExample");
            JPanel p = new GridBagLayoutExample();
            f.getContentPane().add(p , BorderLayout.CENTER);
            f.setSize(230, 230);
            f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            f.setVisible(true);
        }
     
    }
      

  2.   

    哦,您这样做 !
    我曾经试过您的方法,不过,您不觉得new 太多了么?