[求助]不知错那里
import javax.swing.JFrame;import java.awt.CardLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class UseCardLayout extends JFrame{
CardLayout cardLayout1=new CardLayout(2,3);
JButton jButton1=new JButton("Key1");
JButton jButton2=new JButton("Key2");
JButton jButton3=new JButton("Key3");
UseCardLayout(){
super("卡片布局示例");
setSize(300,200);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
this.getContentPane().setLayout(cardLayout1);
this.getContentPane().add("myCard1",jButton1);
this.getContentPane().add("myCard2",jButton2);
this.getContentPane().add("myCard3",jButton3);
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
setResizable(true);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
if(source==jButton1||source==jButton2){
cardLayout1.next(this.getContentPane());
}
else if(e.getSource()==jButton3){
cardLayout1.first(this.getContentPane());
}
}
public static void main(String[] args){
UseCardLayout ucl=new UseCardLayout();
}
}
我运行错误,我不知那里错了!
jButton1.addActionListener(this);
jButton2.addActionListener(this);
jButton3.addActionListener(this);
提示的是这三个事件实现错误码,但我不知为什么?