/**
 * @(#)GUI.java
 *
 * GUI application
 *
 * @author 
 * @version 1.00 2008/9/2
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import  java.applet.*;
class TextAreaDemo extends Frame
{
Box b1=Box.createVerticalBox();
Box b2=Box.createVerticalBox();
public TextAreaDemo()
{
TextField tf_name=new TextField(10);
TextField tf_pwd=new TextField(10);
Label l_name=new Label("name:");
Label l_pwd=new Label("password:");
// b1.add(l_name);
// b1.add(l_pwd);
// b2.add(tf_name);
// b1.add(Box.createVerticalStrut(9));
// b2.add(tf_pwd);
// b1.add(Box.createVerticalStrut(9));
// add(b1);
// add(b2);
Panel box1=new Panel();
Panel box2=new Panel();
box1.setBackground(Color.red);
box1.add(l_name);
box1.add(tf_name);
add(box1);
box2.add(l_pwd);
box2.add(tf_pwd);
add(box2);
}
}
public class GUI {
    
    public static void main(String[] args) 
    {
     int width=200,height=300;
     TextAreaDemo tad=new TextAreaDemo();
     tad.setSize(width,height);
     tad.setLocation(30,40);
     tad.setMaximumSize(new Dimension(width*3,height*2));
     tad.show();
     tad.addWindowListener(new WindowAdapter()
     {
     public void windowClosing(WindowEvent we)
     {
     System.exit(1);
     }
     });
    
    }
}
为什么只显示box1呢?

解决方案 »

  1.   

    Frame的默认布局管理器是BorderLayout
      

  2.   

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.applet.*;class TextAreaDemo extends Frame {    Box b1 = Box.createVerticalBox();
        Box b2 = Box.createVerticalBox();    public TextAreaDemo() {
            TextField tf_name = new TextField(10);
            TextField tf_pwd = new TextField(10);
            Label l_name = new Label("name:");
            Label l_pwd = new Label("password:");
    // b1.add(l_name);
    // b1.add(l_pwd);
    // b2.add(tf_name);
    // b1.add(Box.createVerticalStrut(9));
    // b2.add(tf_pwd);
    // b1.add(Box.createVerticalStrut(9));
    // add(b1);
    // add(b2);
            Panel box1 = new Panel();
            Panel box2 = new Panel();
            box1.setBackground(Color.red);
            box1.add(l_name);
            box1.add(tf_name);
            add(box1,BorderLayout.SOUTH);
            box2.add(l_pwd);
            box2.add(tf_pwd);
            add(box2,BorderLayout.CENTER);
        }
    }public class Main {    public static void main(String[] args) {
            int width = 200, height = 300;
            TextAreaDemo tad = new TextAreaDemo();
            tad.setSize(width, height);
            tad.setLocation(30, 40);
            tad.setMaximumSize(new Dimension(width * 3, height * 2));
            tad.show();
            tad.addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent we) {
                    System.exit(1);
                }
            });    }
    }
      

  3.   

    Frame的默认布局管理器是BorderLayout,
    不指定具体位置当然覆盖了。
      

  4.   

    package ChatClient03;import java.awt.*; 
    import javax.swing.*; 
    import java.awt.event.*; 
    import  java.applet.*; 
    class TextAreaDemo extends Frame 

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    Box b1=Box.createVerticalBox(); 
    Box b2=Box.createVerticalBox(); 
    public TextAreaDemo() 

    TextField tf_name=new TextField(10); 
    TextField tf_pwd=new TextField(10); 
    Label l_name=new Label("name:"); 
    Label l_pwd=new Label("password:"); 
    // b1.add(l_name); 
    // b1.add(l_pwd); 
    // b2.add(tf_name); 
    // b1.add(Box.createVerticalStrut(9)); 
    // b2.add(tf_pwd); 
    // b1.add(Box.createVerticalStrut(9)); 
    // add(b1); 
    // add(b2); 
    this.setLayout(new BorderLayout()) ;
    Panel box1=new Panel(); 
    Panel box2=new Panel(); 
    box1.setBackground(Color.red); 
    box1.add(l_name); 
    box1.add(tf_name); 
    add(box1,BorderLayout.NORTH); 
    box2.add(l_pwd); 
    box2.add(tf_pwd); 
    add(box2,BorderLayout.CENTER); 


    public class GUI { 
        
        public static void main(String[] args) { 
        int width=200,height=300; 
        TextAreaDemo tad=new TextAreaDemo(); 
        tad.setSize(width,height); 
        tad.setLocation(30,40); 
        tad.setMaximumSize(new Dimension(width*3,height*2)); 
    //    tad.show(); 
        tad.setVisible(true) ;
        tad.addWindowListener(new WindowAdapter(){
          public void windowClosing(WindowEvent we)  { 
                 System.exit(1); 
                 } 
                 
        }); 
       
        }
    }