1. 程式人生 > >Java(41)_卡片佈局管理器

Java(41)_卡片佈局管理器

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(); } }