public class TestOfPack extends JFrame {
 public TestOfPack() {
     addWindowListener(new WindowAdapter()
           {  public void windowClosing(WindowEvent e)
              {  System.exit(0);
              }
           } );     System.out.print(getLayout());
 
     getContentPane().add(new Label("Hello"));
     getContentPane().add(new TextField("Hello") );
     getContentPane().add(new Button("Hello") );
 //validate();
 pack();
 show();
 } public static void main(String args[]) {
 new TestOfPack ();
 }
}为什么结果中只有一个按钮呢?

解决方案 »

  1.   

    谢谢。我只看到了按钮,没看到Label和Textfield啊。
      

  2.   

    getContentPane().add(new Label("Hello"),BorderLayout.NORTH);
    getContentPane().add(new TextField("Hello"),BorderLayout.CENTER );
    getContentPane().add(new Button("Hello"),BorderLayout.SOUTH );
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    BorderLayout把一个pane分成东西南北中五个位置,默认的时中。你没写,三个都摞到一起去了。
      

  3.   

    记得有一本书上说,幸亏borderlayout的作者不是船员出身,否则,把五个位置取名位“前甲板、后甲板、左舷、右舷、船舱”……