import java.awt.*;public class FrameWithPanel extends Frame
{
public static void main(String[] args)
{
FrameWithPanel fr = new FrameWithPanel("Hello!"); fr.setSize(200, 200);
fr.setBackground(Color.blue);
fr.setLayout(new GridLayout(2, 1)); Panel pan = new Panel();
pan.setSize(200, 100);
pan.setBackground(Color.yellow);
pan.add(new Button("确定"));
fr.add(pan); fr.setVisible(true);
} public FrameWithPanel(String str)
{
super(str);
}
}这个程序如果这么写,显示的是上下两部分,再看下面这段程序import java.awt.*;public class FrameWithPanel extends Frame
{
public static void main(String[] args)
{
FrameWithPanel fr = new FrameWithPanel("Hello!"); fr.setSize(200, 200);
fr.setBackground(Color.blue);
fr.setLayout(new GridLayout(1, 2)); Panel pan = new Panel();
pan.setSize(100, 200);
pan.setBackground(Color.yellow);
pan.add(new Button("确定"));
fr.add(pan); fr.setVisible(true);
} public FrameWithPanel(String str)
{
super(str);
}
}我希望能够显示左右两部分,但为什么不是显示左右两部分呢?

解决方案 »

  1.   

    fr.setLayout(new GridLayout(0, 1));
      

  2.   

    fr.setLayout(new GridLayout(0, 2));写成0行2列就可以了!
      

  3.   

    你的程序中写成两行一列了吧,我觉得写成 fr.setLayout(new GridLayout(2, 1));
    为什么要写成fr.setLayout(new GridLayout(0, 2)); 不明白,也没试。
      

  4.   

    (0,2)与(1,2)是没有多大区别的。但是在你这里显示肯定不是你要的效果。因为你的第二列只是背景颜色。你又只添加了一个panal中一列中。这列肯定会覆盖总行的。
    而(2,1)和(2.0)几乎是完全一样了。就不会出现你的第一行的东西跑到第二行去的。