你把GridBagLayout当成XYLayout来用了,GridBagLayout应该跟GridBagConstraints配合使用,具体见例子(来自jdk文档):
 import java.awt.*;
 import java.util.*;
 import java.applet.Applet; public class GridBagEx1 extends Applet {     protected void makebutton(String name,
                               GridBagLayout gridbag,
                               GridBagConstraints c) {
         Button button = new Button(name);
         gridbag.setConstraints(button, c);
         add(button);
     }     public void init() {
         GridBagLayout gridbag = new GridBagLayout();
         GridBagConstraints c = new GridBagConstraints();         setFont(new Font("Helvetica", Font.PLAIN, 14));
         setLayout(gridbag);         c.fill = GridBagConstraints.BOTH;
         c.weightx = 1.0;
         makebutton("Button1", gridbag, c);
         makebutton("Button2", gridbag, c);
         makebutton("Button3", gridbag, c);         c.gridwidth = GridBagConstraints.REMAINDER; //end row
         makebutton("Button4", gridbag, c);         c.weightx = 0.0;    //reset to the default
         makebutton("Button5", gridbag, c); //another row     c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
         makebutton("Button6", gridbag, c);     c.gridwidth = GridBagConstraints.REMAINDER; //end row
         makebutton("Button7", gridbag, c);     c.gridwidth = 1;        //reset to the default
     c.gridheight = 2;
         c.weighty = 1.0;
         makebutton("Button8", gridbag, c);         c.weighty = 0.0;    //reset to the default
     c.gridwidth = GridBagConstraints.REMAINDER; //end row
     c.gridheight = 1;    //reset to the default
         makebutton("Button9", gridbag, c);
         makebutton("Button10", gridbag, c);         setSize(300, 100);
     }     public static void main(String args[]) {
     Frame f = new Frame("GridBag Layout Example");
     GridBagEx1 ex1 = new GridBagEx1();     ex1.init();     f.add("Center", ex1);
     f.pack();
     f.setSize(f.getPreferredSize());
     f.show();
     }
 }

解决方案 »

  1.   

    Applet默认的是流式布局,也就是FlowLayout,你把它改成GridLayout就可以了.
    也就是加上setLayout(new GridLayout());
      

  2.   

    To  jinus(燃冰) :
      你的程序运行的结果是按钮把整个屏幕填满了,但是我只是要他用前4行,剩下的地方我还好画图。
    To  noratong(诺拉):
      按你的说法改的程序如果用.html文件直接打开,还是一行按钮出现在中间;如果用Appletviewer打开,就成了每个按钮占一列,每一列都有整个屏幕那么高,还是一行,而且全屏幕,不能留出地方画图啊。
      

  3.   

    刚才我察了察API好像没有XYLayout 啊
      

  4.   

    忘记高让你在里面加参数了.GridLayout里面要带两个参数,一个列的个数,一个行的个数.按你画出的XXXX的图来看,应该要设置5列.但行数是不是你设置的4行我就不清楚了.让后依次添加,你的布局就好像一个网格样的,在的需要的单元格的位置就添加按钮,不需要的位置就添加其它组件.一个add方法就表示添加了一个单元格.它的添加顺序是从左到右,从上到下一次添加的.我看你好像对布局很不熟悉呢,建议多看书.
      

  5.   

    setLayout(new GridLayout(),9,100);
    但是编译时报错:Wrong number of arguments in method.
      

  6.   

    我的天,你一点都不懂GridLayout布局呀????
    应该是setLayout(new GridLayout(9,100));
    行数和列数的参数是传入给GridLayout的,不是给容器的.
    多看一下书,讲基础的书上都有布局的介绍和用法.