📄 cardlayoutdemo.java
字号:
import java.awt.*;
import java.awt.event.*;
public class CardLayoutDemo extends Frame
{
//包含四个功能按钮的Panel的定义和创建
Panel pnlCommandArea=new Panel();
//显示功能Panel的定义和创建
Panel pnlDisplayArea=new Panel();
//CardLayout布局管理器的创建
CardLayout cardlayout1=new CardLayout();
//四个功能按钮的定义和创建
Button btnFirst=new Button("第一个");
Button btnPrevious=new Button("前一个");
Button btnNext=new Button("后一个");
Button btnLast=new Button("最后一个");
//框架窗体的构造方法
public CardLayoutDemo()
{
//设置Frame的布局管理器为BorderLayout
this.setLayout(new BorderLayout());
//把两个Panel加入到布局管理器中
this.add( pnlCommandArea, BorderLayout.NORTH);
this.add( pnlDisplayArea, BorderLayout.CENTER);
pnlDisplayArea.setLayout(cardlayout1);
//创建第一个显示Panel
Panel pnlFirst=new Panel();
pnlFirst.setBackground(Color.blue);
pnlFirst.setForeground(Color.white);
pnlDisplayArea.add("first",pnlFirst);
pnlFirst.add(new Label("This is the first Panel") );
//创建第二个显示Panel
Panel pnlSecond=new Panel();
pnlSecond.setBackground(Color.red);
pnlSecond.setForeground(Color.blue);
pnlDisplayArea.add("second",pnlSecond);
pnlSecond.add(new Label("This is the second Panel") );
//创建第三个显示Panel
Panel pnlThird=new Panel();
pnlThird.setBackground(Color.black);
pnlThird.setForeground(Color.white);
pnlDisplayArea.add("third",pnlThird);
pnlThird.add(new Label("This is the third Panel") );
//创建第四个显示Panel
Panel pnlFourth=new Panel();
pnlFourth.setBackground(Color.green);
pnlFourth.setForeground(Color.black);
pnlDisplayArea.add("fourth",pnlFourth);
pnlFourth.add(new Label("This is the fourth Panel") );
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 )
{
//创建框架窗体的实例
CardLayoutDemo f=new CardLayoutDemo();
//设置框架窗体的事件监听(关闭窗体事件)
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
//正常退出Java虚拟机
System.exit(0);
}
});
//显示框架窗体
f.pack();
f.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 + -