面板里面添加面板,应该是可以的,下面程序可以编译,但不知道为什么不能用运行呀?import java.awt.*;
import java.awt.event.*;class Mypanel1 extends Panel  
{  
Label L;   Mypanel1()  
   { L=new Label("欢迎使班级管理系统");
   L.setBackground(Color.pink);
   add(L);
   }
}
class Mypanel2 extends Panel 
{   String s1,s2;
    
    TextArea text;
    Button button1,button2;
Mypanel2()
{     setBackground(Color.cyan);
      button1=new Button("显示自己的信息");
      button2=new Button("更多");
      add(button1,BorderLayout.NORTH);
      add(button2,BorderLayout.NORTH);
      add(text,BorderLayout.CENTER);
}}
class WinCard extends Frame 
{    int index;
 CardLayout card;
     Button button1,button2;
     Label L1,L2,L3;
     TextField number,key;
     Choice ID;
     Mypanel1 p1; Mypanel2 p2;
     Panel panel1,panel2;  //出错的地方,我把上面p1,p2添加到panel1中,把这两个面板去掉就可以运行,为什么?
   WinCard() 
   {  card=new CardLayout();
      panel1=new Panel();
      p1=new Mypanel1();
      p2=new Mypanel2();
     ID=new Choice();
     ID.add("学生");
     ID.add("老师");
     ID.add("管理员");
      button1=new Button("连接到服务器");
      button2=new Button("登陆");
      L1=new Label("学号或工号");
      L2=new Label("密码 ");
      L3=new Label("请选择身份");
      String a,b,c;
      panel1.setLayout(card); 
       panel1.add("a",p1);
      panel1.add("b",p2);
      
      panel2.setLayout(new FlowLayout());
      panel2.add(button1);
      panel2.add(number);
      panel2.add(key);
      panel2.add(ID);
      panel2.add(button2);
  
      add(panel1,BorderLayout.CENTER);
      add(panel2,BorderLayout.SOUTH);
      addWindowListener(new WindowAdapter()
      { public void windowClosing(WindowEvent e)
           {  System.exit(0);
            }
      });
      setBounds(10,10,200,190);
      setVisible(true);
      validate();
   }
    }public class Test
{  public static void main(String args[])
   { new WinCard();
   }
}

解决方案 »

  1.   

    答:面板里面添加面板,应该是可以的,下面程序可以编译,但不知道为什么不能用运行呀? 
    与"面板里面添加面板"没有任何关系.错误原因是:几个组件楼主没有初始化引起的.
    参考代码如下:(能正常运行,看到你的界面了)import java.awt.*; 
    import java.awt.event.*; class Mypanel1 extends Panel  
    {  
    Label L;   Mypanel1()  
      { L=new Label("欢迎使班级管理系统"); 
      L.setBackground(Color.pink); 
      add(L); 
      } 

    class Mypanel2 extends Panel 
    {  String s1,s2; 
        
        TextArea text; 
        Button button1,button2; 
    Mypanel2() 
    {    setBackground(Color.cyan); 
          button1=new Button("显示自己的信息"); 
          button2=new Button("更多"); 
          text=new TextArea(4,80);
          add(button1,BorderLayout.NORTH); 
          add(button2,BorderLayout.NORTH); 
          add(text,BorderLayout.CENTER); 
    } } 
    class WinCard extends Frame 
    {    int index; 
    CardLayout card; 
        Button button1,button2; 
        Label L1,L2,L3; 
        TextField number,key; 
        Choice ID; 
        Mypanel1 p1; Mypanel2 p2; 
        Panel panel1,panel2;  //出错的地方,我把上面p1,p2添加到panel1中,把这两个面板去掉就可以运行,为什么? 
      WinCard() 
      {  card=new CardLayout(); 
          panel1=new Panel(); 
          panel2=new Panel(); //加上
          p1=new Mypanel1(); 
          p2=new Mypanel2(); 
        ID=new Choice(); 
        ID.add("学生"); 
        ID.add("老师"); 
        ID.add("管理员"); 
          button1=new Button("连接到服务器"); 
          button2=new Button("登陆"); 
          L1=new Label("学号或工号"); 
          L2=new Label("密码 "); 
          L3=new Label("请选择身份"); 
          String a,b,c; 
          panel1.setLayout(card); 
          panel1.add("a",p1); 
          panel1.add("b",p2); 
          
          panel2.setLayout(new FlowLayout()); 
          panel2.add(button1); 
          number=new TextField(10);
          key=new TextField(10);
          panel2.add(number); 
          panel2.add(key); 
          panel2.add(ID); 
          panel2.add(button2); 
      
          add(panel1,BorderLayout.CENTER); 
          add(panel2,BorderLayout.SOUTH); 
          addWindowListener(new WindowAdapter() 
          { public void windowClosing(WindowEvent e) 
              {  System.exit(0); 
                } 
          }); 
          setBounds(10,10,400,190); 
          setVisible(true); 
          validate(); 
      } 
        } public class Test 
    {  public static void main(String args[]) 
      { new WinCard(); 
      } 
      

  2.   

    你落了很多东西
    许多都没初始化
    import java.awt.*; 
    import java.awt.event.*; class Mypanel1 extends Panel
    {
    Label L;  Mypanel1()  

    L=new Label("欢迎使班级管理系统"); 
    L.setBackground(Color.pink); 
    add(L); 

    }class Mypanel2 extends Panel 
    {
    String s1,s2; 
    TextArea text; 
        Button button1,button2; 

    Mypanel2() 
    {    
    setBackground(Color.cyan); 
    button1=new Button("显示自己的信息"); 
    button2=new Button("更多"); 
    text = new TextArea();//加上
    add(button1,BorderLayout.NORTH); 
    add(button2,BorderLayout.NORTH); 
    add(text,BorderLayout.CENTER); 
    }

    class WinCard extends Frame 
    {    
    int index; 
    CardLayout card; 
        Button button1,button2; 
        Label L1,L2,L3; 
        TextField number,key; 
        Choice ID; 
        Mypanel1 p1; Mypanel2 p2; 
        Panel panel1,panel2;  //出错的地方,我把上面p1,p2添加到panel1中,把这两个面板去掉就可以运行,为什么? 

    WinCard() 
    {
    card=new CardLayout(); 
    panel1=new Panel(); 
    p1=new Mypanel1(); 
    p2=new Mypanel2(); 
    ID=new Choice(); 
    ID.add("学生"); 
    ID.add("老师"); 
    ID.add("管理员"); 
    button1=new Button("连接到服务器"); 
    button2=new Button("登陆"); 
    L1=new Label("学号或工号"); 
    L2=new Label("密码 "); 
    L3=new Label("请选择身份"); 
    String a,b,c; 
    panel1.setLayout(card); 
    panel1.add("a",p1); 
    panel1.add("b",p2); 
          
    panel2 = new Panel();//加上
    number = new TextField();//加上
    key = new TextField();//加上
    panel2.setLayout(new FlowLayout()); 
    panel2.add(button1); 
    panel2.add(number); 
    panel2.add(key); 
    panel2.add(ID); 
    panel2.add(button2); 
      
    add(panel1,BorderLayout.CENTER); 
    add(panel2,BorderLayout.SOUTH); 
    addWindowListener(new WindowAdapter() 
    { public void windowClosing(WindowEvent e) 
              {  System.exit(0);} 
    }); 
    setBounds(10,10,200,190); 
    setVisible(true); 
    validate(); 

    } public class Test 
    {  
    public static void main(String args[]) 

    WinCard win = new WinCard();