import java.awt.CardLayout;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CardLayOutTest
{
public static void main(String[] args)
{
CardLayOutFrame frame = new CardLayOutFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class CardLayOutFrame extends JFrame
{
public CardLayOutFrame()
{
setTitle("卡片布局管理器");
setSize(300, 200);
Container con = getContentPane();
CardLayOutPanel panel = new CardLayOutPanel();
con.add(panel);
}
}
class CardLayOutPanel extends JPanel
{
public CardLayOutPanel()
{
//实例化卡片布局管理器
CardLayout layout = new CardLayout();
//设置布局管理器
setLayout(layout);
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
add(b1,"card1");
add(b2,"card2");
add(b3,"card3");
add(b4,"card4");
add(b5,"card5");
layout.first(CardLayOutPanel.this);//第一张
layout.last(CardLayOutPanel.this);//最后一张
layout.previous(CardLayOutPanel.this);//上一张
layout.next(CardLayOutPanel.this);//下一张
layout.show(CardLayOutPanel.this,"card3");//根据字符串显示特定卡片
}
}