http://
/*
2008年10月17日9:10:21
在一个窗口的正中间显示另一个窗口,
有关布局管理器的问题
*/import java.awt.*;public class CenterPanel {
  public static void main(String args[]) {
    new MyFrame3(300,300,400,400,Color.BLUE);
  }
}class MyFrame3 extends Frame{
  private Panel p;
  MyFrame3(int x,int y,int w,int h,Color c){
    super("FrameWithPanel");
    this.setLayout(new FlowLayout());
setBounds(x,y,w,h);
    setBackground(c);
    p = new Panel(null); //把null去掉了,则输出没看到任何影响 
    p.setBounds(1000,1000,w/2, h/2); //从JDK 1.6中的运行结果发现黄色小窗口的位置并不是在距离父窗口【1000,1000】的位置,  也就是说第一个参数和第二个参数不起作用,只有第三和第四和参数才起作用,即只能确定窗口的宽和高,但无法确定窗口的位置,为何????
    p.setBackground(Color.YELLOW);
    add(p);
    setVisible(true);
  }
}

解决方案 »

  1.   


    import java.awt.*; public class CenterPanel { 
      public static void main(String args[]) { 
        new MyFrame3(300,300,400,400,Color.BLUE); 
      } 
    } class MyFrame3 extends Frame{ 
      private Panel p; 
      MyFrame3(int x,int y,int w,int h,Color c){ 
        super("FrameWithPanel"); 
        //this.setLayout(new FlowLayout()); 
        this.setLayout(null); //你原来把布局管理器设置为了FlowLayout   可以认为它会无视你的坐标值,设置为null 你就可以用坐标定位了
        setBounds(x,y,w,h); 
        setBackground(c); 
        p = new Panel(null); //这里的null 是 把p的布局管理器设置为null了 也就是不用布局管理器了
        p.setBounds(10,10,w/2, h/2); 
        p.setBackground(Color.YELLOW); 
        add(p); 
        setVisible(true); 
      }