日期:2014-05-20 浏览次数:20810 次
import java.awt.*; import java.awt.event.*; public class CardLayoutDemo extends Frame implements MouseListener{ private Button first=new Button("A"); private Button second=new Button("B"); private Button third=new Button("C"); private CardLayout cl=new CardLayout(); private Panel cards=new Panel(); public CardLayoutDemo(String string){ super(string); init(); //调用init方法 } public void init(){ setLayout(new BorderLayout()); setSize(500, 500); Panel p=new Panel(); p.setLayout(new FlowLayout()); first.addMouseListener(this); second.addMouseListener(this); third.addMouseListener(this); p.add(first); p.add(second); p.add(third); add("North",p); cards.setLayout(cl); cards.add("First cards",new Button("AA")); cards.add("second Button",new Button("BB")); cards.add("third cards",new Button("CC")); add("Center",cards); addWindowListener(new WindowAdapter(){ public void WindowClosing(WindowEvent evt){ setVisible(false); dispose(); System.exit(0); } }); setVisible(true); } public static void main(String[] args) { new CardLayoutDemo("ABCD"); //创建CardLayoutDemo类型变量 } public void mouseClicked(MouseEvent evt){ if(evt.getSource()==first){ cl.first(cards); } else if(evt.getSource()==second){ cl.first(cards); cl.next(cards); } else if(evt.getSource()==third){ cl.last(cards); } } public void mouseEntered(MouseEvent arg0){ } public void mouseExited(MouseEvent arg0){ } public void mousePressed(MouseEvent arg0){ } public void mouseReleased(MouseEvent arg0){ } }