点击按钮时eclipse报错,内容页不能切换是不是监听器部分的代码写的有问题,我对监听器的运用不熟悉求高手指正。
代码:
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MyTestCardLayout extends JFrame implements ActionListener{
public MyTestCardLayout(){
super("MyTestCardLayout");
}
CardLayout cardLayout = new CardLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
public void myTestCardLayout(){
Button button1 = new Button("1");
Button button2 = new Button("2");
Button button3 = new Button("3");
Button button4 = new Button("4");
Button button5 = new Button("5");
Button button6 = new Button("7");
Button button7 = new Button("8");
Button button8 = new Button("下一页");
Button button9 = new Button("尾页");
jPanel1.add(button1);
jPanel1.add(button2);
jPanel1.add(button3);
jPanel1.add(button4);
jPanel1.add(button5);
jPanel1.add(button6);
jPanel1.add(button7);
jPanel1.add(button8);
jPanel1.add(button9);
jPanel2.setLayout(new CardLayout());
jPanel2.add(new JLabel("这是第一页"),"1");
jPanel2.add(new JLabel("这是第二页"),"2");
jPanel2.add(new JLabel("这是第三页"),"3");
jPanel2.add(new JLabel("这是第四页"),"4");
jPanel2.add(new JLabel("这是第五页"),"5");
jPanel2.add(new JLabel("这是第六页"),"6");
jPanel2.add(new JLabel("这是第七页"),"7");
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
add(jPanel1,BorderLayout.SOUTH);
add(jPanel2,BorderLayout.CENTER);
setLocation(500, 300);
setVisible(true);
setSize(400,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if("1".equals(str))