求助:卡片布局出现: wrong parent for cardlayout
import java.awt.*;import java.awt.event.*;
public class smp610 extends Frame implements ActionListener
{
static Panel pnl=new Panel();
static CardLayout crd=new CardLayout(20,15);
Cvs cvs[]=new Cvs[10];
Button button1=new Button("第一幅");
Button button2=new Button("下一幅");
Button button3=new Button("上一幅");
Button button4=new Button("第末幅");
smp610()
{
super("CardLayout布局");
setSize(300,250);
setBackground(Color.green);
setVisible(true);
setLayout(null);
add(pnl);
pnl.setBounds(0,40,200,190);
pnl.setBackground(Color.blue);
for(int k=0;k<=9;k++)
{
cvs[k]=new Cvs(k);
pnl.add(""+k,cvs[k]);
}
add(button1);
add(button2);
add(button3);
add(button4);
button1.setBounds(230,60,50,30);
button2.setBounds(230,100,50,30);
button3.setBounds(230,140,50,30);
button4.setBounds(230,180,50,30);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
crd.first(pnl);
}
else if(e.getSource()==button2)
{
crd.next(pnl);
}
else if(e.getSource()==button3)
{
crd.previous(pnl);
}
else if(e.getSource()==button4)
{
crd.last(pnl);
}
}
class Cvs extends Canvas
{
int i;
Cvs(int j)
{
i=j;
setBackground(Color.yellow);
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillRect(30,25,10+10*i,8+8*i);
g.drawString("这是第"+(i+1)+"个画面",20,150);
}
}
public static void main(String args[])
{
smp610 smp=new smp610();
crd.first(pnl);
}
}