import java.awt.*;
import java.awt.event.*;
 
class NestPanel implements ActionListener
{
Button pre,nex;
Color [] co1={Color.red,Color.blue,Color.green};
Color [] co2={Color.orange,Color.black,Color.white};
Button [] colorbutton_1 ={new Button("2"),new Button("1"),new Button("3")};
Button [] colorbutton_2 ={new Button("5"),new Button("15"),new Button("31")};
Panel main_1;
Panel carrier_1;
Panel carrier_2;
CardLayout cardl;
NestPanel()
{
Frame f=new Frame();
f.setVisible(true);
f.setSize(300,200);
main_1=new Panel();//总卡[片
pre=new Button("previous");
nex=new Button("next");
main_1.add(pre);
main_1.add(nex);
f.add(main_1);
//开始布局,卡片布局
 cardl=new CardLayout ();
main_1.setLayout(cardl);
for (int i=0;i<3;i++)
{
colorbutton_1[i].setBackground(co1[i]);//设置按钮上的颜色 }
carrier_1=new Panel();//卡片一
for (int i=0;i<3;i++)//为卡片一天加按钮
{
carrier_1.add(colorbutton_1 [i]);
} carrier_2=new Panel();//卡片二
for (int i=0;i<3;i++)//为卡片二添加按钮
{
carrier_2.add(colorbutton_2 [i]);
}
main_1.add(carrier_1,"c1");
main_1.add(carrier_2,"c2");//将卡片添加到总卡片,其上是颜色按钮
//设置按钮监听器
pre.addActionListener(this);
nex.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
 {
  if(ae.getSource==pre)
  cardl.show(main_1,"c1");
  else
  cardl.show(main_1,"c2");
 
 }
public static void main(String args [])
{
NestPanel n=new NestPanel();
}
}为什,左后只出现了一个空的无内容的FRAME!button,panel都没了!

解决方案 »

  1.   

    package LastMonth;import java.awt.*;
    import java.awt.event.*;
     
    class NestPanel implements ActionListener
    {
    Button pre,nex;
    Color [] co1={Color.red,Color.blue,Color.green};
    Color [] co2={Color.orange,Color.black,Color.white};
    Button [] colorbutton_1 ={new Button("2"),new Button("1"),new Button("3")};
    Button [] colorbutton_2 ={new Button("5"),new Button("15"),new Button("31")};
    Panel main_1;
    Panel carrier_1;
    Panel carrier_2;
    CardLayout cardl;
    NestPanel()
    {
    Frame f=new Frame();
    f.setVisible(true);
    f.setSize(300,200);
    main_1=new Panel();//总卡[片
    pre=new Button("previous");
    nex=new Button("next");
    main_1.add(pre);
    main_1.add(nex);
    f.add(main_1);
    //开始布局,卡片布局
     cardl=new CardLayout ();
    main_1.setLayout(cardl);
    for (int i=0;i<3;i++)
    {
    colorbutton_1[i].setBackground(co1[i]);//设置按钮上的颜色}
    carrier_1=new Panel();//卡片一
    for (int i=0;i<3;i++)//为卡片一天加按钮
    {
    carrier_1.add(colorbutton_1 [i]);
    }carrier_2=new Panel();//卡片二
    for (int i=0;i<3;i++)//为卡片二添加按钮
    {
    carrier_2.add(colorbutton_2 [i]);
    }
    main_1.add(carrier_1,"c1");
    main_1.add(carrier_2,"c2");//将卡片添加到总卡片,其上是颜色按钮
    //设置按钮监听器
    pre.addActionListener(this);
    nex.addActionListener(this);
    f.validate();
    //f.pack();
    }
    public void actionPerformed(ActionEvent ae)
     {
     if(ae.getSource()==pre)
     cardl.show(main_1,"c1");
     else
     cardl.show(main_1,"c2");
     
     }
    public static void main(String args [])
    {
    NestPanel n=new NestPanel();
    }
    }