import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class demo_9
{
public static void main(String [] args)
{
demo_9_1 test=new demo_9_1();
}
}
class demo_9_1 extends JFrame //implements ActionListener
{
JPanel cardPanel=new JPanel();//设置card的面板
JPanel buttonPanel=new JPanel();//设置按钮的放置的面板
JButton j0,j1,j2,j3;
CardLayout card =new CardLayout();
String cardPanelName[]={"0","1","2","3"};
public demo_9_1()
{
/*
*
*jframe窗体的布局管理器默认为borederlayout 方法进行布局的
*所以可以使用aa.add(cardPanel,BorderLayout.CENTER)
*/
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(0,0,400,300);
cardPanel.setLayout(card);
Container aa=this.getContentPane();//返回的是本窗体的容器
aa.add(cardPanel,BorderLayout.CENTER);//将面板添加到窗体的中心位置
aa.add(buttonPanel,BorderLayout.SOUTH);//将面板添加到窗体的南部位置
j0=new JButton("0");
j1=new JButton("1");
j2=new JButton("2");
j3=new JButton("3");
buttonPanel.add(j0);
buttonPanel.add(j1);
buttonPanel.add(j2);
buttonPanel.add(j3);
for(int i=0;i<4;i++)
{
cardPanel.add(cardPanelName[i],new JButton("按钮"+i));//循环将按钮加入到设置好了的面板中,//带有字符串的面板将指定组件添加到此容器中
}
j0.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
card.show(cardPanel,cardPanelName[0]);
}
}
);
j1.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
card.show(cardPanel,cardPanelName[1]);
}
}
);
j2.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
card.show(cardPanel,cardPanelName[2]);
}
}
);
j3.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
card.show(cardPanel,cardPanelName[3]);
}
}
);
}
// public void actionPerformed(ActionEvent e)
// {
// card.show(cardPanel,cardPanelName[0]);
// }
}