import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutDemo extends JFrame implements ActionListener{
private CardLayout card;
private BorderLayout border;
private Container cp;
private JPanel upPanel,downPanel;
private JButton firstButton,nextButton,prevButton,lastButton;
private JLabel oneLabel,twoLabel,threeLabel,fourLabel;
public CardLayoutDemo(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(350,120);
setTitle("CardLayoutDemo");
card = new CardLayout();//创建一个卡片布局管理器
border = new BorderLayout();//创建一个边界布局管理器
upPanel = new JPanel();//创建面板对象upPanel
downPanel = new JPanel();//创建面板对象downPanel
firstButton = new JButton("第一张");
prevButton = new JButton("前一张");
nextButton = new JButton("后一张");
lastButton = new JButton("最后一张");
oneLabel = new JLabel("这是第一张标签卡片",JLabel.CENTER);//标签内容居中显示
twoLabel = new JLabel("这是第二张标签卡片",JLabel.CENTER);
threeLabel = new JLabel("这是第三张标签卡片",JLabel.CENTER);
fourLabel = new JLabel("这是第四张标签卡片",JLabel.CENTER);
cp = getContentPane();
cp.setLayout(border);//设置内容面板为边界布局
upPanel.setLayout(card);//将upPanel设置为卡片布局
upPanel.setBorder(BorderFactory.createMatteBorder(1,1,2,2,Color.red));//添加面板边框
upPanel.add("one",oneLabel);//向卡片布局的面板添加标签,并附带识别字符串"one"
upPanel.add("two",twoLabel);
upPanel.add("three",threeLabel);
upPanel.add("four",fourLabel);
cp.add(upPanel,"Center");//将面板upPanel添加到窗口的内容面板中
downPanel.add(firstButton);
downPanel.add(prevButton);
downPanel.add(nextButton);
downPanel.add(lastButton);
cp.add(downPanel,"South");
firstButton.addActionListener(this);
nextButton.addActionListener(this);
prevButton.addActionListener(this);
lastButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==firstButton)
card.first(upPanel);//显示面板中的第一个卡片(标签对象)
else if(e.getSource()==nextButton)
card.next(upPanel);
else if(e.getSource()==prevButton)
card.previous(upPanel);
else if(e.getSource()==lastButton)
card.last(upPanel);
}
public static void main(String[] args){
JFrame Frame = new CardLayoutDemo();
Frame.show();
}
}
其中的public void actionPerformed这段看不懂 前面也没有对first next previous last 的定义 在这里又是何意呢?

解决方案 »

  1.   

    CardLayout类里有这样的方法啊.你去看看JDK帮助文档吧first
    public void first(Container parent)翻转到容器的第一张卡片。 参数:
    parent - 要在其中进行布局的父容器
    另请参见:
    last(java.awt.Container)--------------------------------------------------------------------------------next
    public void next(Container parent)翻转到指定容器的下一张卡片。如果当前的可见卡片是最后一个,则此方法翻转到布局的第一张卡片。 参数:
    parent - 要在其中进行布局的父容器
    另请参见:
    jjava.awt.CardLayout#previous--------------------------------------------------------------------------------previous
    public void previous(Container parent)翻转到指定容器的前一张卡片。如果当前的可见卡片是第一个,则此方法翻转到布局的最后一张卡片。 参数:
    parent - 要在其中进行布局的父容器
    另请参见:
    next(java.awt.Container)--------------------------------------------------------------------------------last
    public void last(Container parent)翻转到容器的最后一张卡片。 参数:
    parent - 要在其中进行布局的父容器
    另请参见:
    first(java.awt.Container)--------------------------------------------------------------------------------