不好意思,第一次,代码不不知道传哪里去了,再发!
import java.awt.*;
import java.awt.event.*;
public class Testcardlayout{
Panel p1,p2,p3,p4;
Label l1,l2,l3,l4;
CardLayout mycard;
Frame f;
public static void main(String args[])
{
Testcardlayout c=new Testcardlayout();
c.go();
}
public void go()
{
f=new Frame("Cardlayout");
mycard=new CardLayout();
f.setLayout(mycard);//设置CARDLAYOUT 布局方式
p1=new Panel();
p2=new Panel();
p3=new Panel();
p4=new Panel();
l1=new Label("This is the first panel");
p1.setBackground(Color.yellow);
p1.add(l1);
l2=new Label("This is the second panel");
p2.setBackground(Color.red);
p2.add(l2);
l3=new Label("This is the thrid panel");
p3.setBackground(Color.green);
p3.add(l3);
l4=new Label("This is the fourth panel");
p4.setBackground(Color.black);
p4.add(l4);
f.add(p1,"First");
f.add(p2,"Second");
f.add(p3,"Thrid");
f.add(p4,"Fourth");
mycard.show(f,"Thrid");//显示第一张卡片
f.addMouseListener(new MouseAdapter(){
//用MouseAdapter的适配器来监听FRAME上的事件
public void mouseClick(MouseEvent e)
{
mycard.show(f,"Second");
}
});
f.setSize(300,300);
f.setVisible(true);
f.show();
}
}