package MYSQK.example01; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /** * 卡片布局管理器 */ class Layout extends Frame implements ActionListener { CardLayout cardLayout = new CardLayout();//定义卡片布局管理器 Panel cardPanel = new Panel();//定义面板放卡片 Panel controlPanel = new Panel();//定义面板放置按钮 Button preButton;//按钮声明 Button nextButton; public Layout(){ this.setSize(300,200);//设置窗体大小 cardPanel.setLayout(cardLayout);//设置布局为卡管理器 把cardPanel,而不是当前窗体!!!this cardPanel.add(new Label("FistCard",Label.CENTER)); cardPanel.add(new Label("SecondCard",Label.CENTER)); cardPanel.add(new Label("ThirdCard",Label.CENTER)); nextButton = new Button("Next"); preButton = new Button("Back"); controlPanel.add(preButton); controlPanel.add(nextButton); this.add(cardPanel,BorderLayout.CENTER); this.add(controlPanel,BorderLayout.SOUTH); //为按钮添加事件监听器 nextButton.addActionListener(this); preButton.addActionListener(this); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ Layout.this.dispose(); } }); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e){ // 如果用户点击向后按钮 if(e.getSource()==nextButton){ cardLayout.next(cardPanel); } if(e.getSource()==preButton){ cardLayout.previous(cardPanel); } } } public class example01 { public static void main(String[] args){ Layout layout = new Layout(); } }