?? cardlayoutdemo.java
字號:
//CardLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
public class CardLayoutDemo extends Frame
{
//包含四個功能按鈕的Panel的定義和創(chuàng)建
Panel pnlCommandArea=new Panel();
//顯示功能Panel的定義和創(chuàng)建
Panel pnlDisplayArea=new Panel();
//CardLayout布局管理器的創(chuàng)建
CardLayout cardlayout1=new CardLayout();
//四個功能按鈕的定義和創(chuàng)建
Button btnFirst=new Button("第一個");
Button btnPrevious=new Button("前一個");
Button btnNext=new Button("后一個");
Button btnLast=new Button("最后一個");
//框架窗體的構造方法
public CardLayoutDemo()
{
super("卡片布局管理器");
//四個功能按鈕的顏色設置
btnFirst.setBackground(Color.white);
btnPrevious.setBackground(Color.white);
btnNext.setBackground(Color.white);
btnLast.setBackground(Color.white);
//設置Frame的布局管理器為BorderLayout
this.setLayout(new BorderLayout());
//把兩個Panel加入到布局管理器中
this.add( pnlCommandArea, BorderLayout.NORTH);
this.add( pnlDisplayArea, BorderLayout.CENTER);
//把顯示功能區(qū)域Panel的布局管理器設置為CardLayout
pnlDisplayArea.setLayout(cardlayout1);
//創(chuàng)建第一個顯示Panel
Panel pnlFirst=new Panel();
pnlFirst.setBackground(Color.yellow);
pnlFirst.setForeground(Color.blue);
pnlDisplayArea.add("first",pnlFirst);
pnlFirst.add(new Label("這是第一張卡片") );
//創(chuàng)建第二個顯示Panel
Panel pnlSecond=new Panel();
pnlSecond.setBackground(Color.pink);
pnlSecond.setForeground(Color.blue);
pnlDisplayArea.add("second",pnlSecond);
pnlSecond.add(new Label("這是第二張卡片") );
//創(chuàng)建第三個顯示Panel
Panel pnlThird=new Panel();
pnlThird.setBackground(Color.orange);
pnlThird.setForeground(Color.blue);
pnlDisplayArea.add("third",pnlThird);
pnlThird.add(new Label("這是第三張卡片") );
//創(chuàng)建第四個顯示Panel
Panel pnlFourth=new Panel();
pnlFourth.setBackground(Color.green);
pnlFourth.setForeground(Color.blue);
pnlDisplayArea.add("fourth",pnlFourth);
pnlFourth.add(new Label("這是第四張卡片") );
//為四個功能按鈕設置事件監(jiān)聽器
btnFirst.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
processAction(e);
}
});
btnPrevious.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
processAction(e);
}
});
btnNext.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
processAction(e);
}
});
btnLast.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
processAction(e);
}
});
//把四個功能按鈕加入到Panel
pnlCommandArea.add( btnFirst );
pnlCommandArea.add( btnPrevious );
pnlCommandArea.add( btnNext );
pnlCommandArea.add( btnLast );
}
//程序的入口方法
public static void main( String[] args )
{
//創(chuàng)建框架窗體的實例
CardLayoutDemo frmCardLayout = new CardLayoutDemo();
//設置框架窗體的事件監(jiān)聽(關閉窗體事件)
frmCardLayout.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
//正常退出Java虛擬機
System.exit(0);
}
});
//顯示框架窗體
frmCardLayout.pack();
frmCardLayout.show();
}
//設置框架窗體的大小為300×300
public Dimension getPreferredSize()
{
return new Dimension(300,300);
}
//處理按鈕的事件
private void processAction(ActionEvent e)
{
//獲取事件源(用戶選擇是哪個按鈕)
Button btnEvent=(Button)e.getSource();
if( btnEvent.equals(btnFirst))
cardlayout1.first( pnlDisplayArea );
else if( btnEvent.equals(btnLast))
cardlayout1.last( pnlDisplayArea );
else if( btnEvent.equals(btnPrevious))
cardlayout1.previous( pnlDisplayArea );
else if( btnEvent.equals(btnNext))
cardlayout1.next( pnlDisplayArea );
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -