同志们,这样加监听对象为什么不行呢?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class useCardLayout extends JFrame {
public CardLayout mycard1 = new CardLayout(2,3);//2,3表示与边缘的距离
public JButton bt1= new JButton("key1");
public JButton bt2 = new JButton("key2");
public JButton bt3 = new JButton("key3");
public Container c = this.getContentPane();
useCardLayout(){
super("卡片管理");
setSize(400,300);
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
c.add("mycard1",bt1);
c.add("mycard1",bt2);
c.add("mycard",bt3);
Lis list = new Lis();
bt1.addActionListener(list);
bt2.addActionListener(list);
bt3.addActionListener(list);
setResizable(true);
setVisible(true);
}
public static void main(String args[]){
new useCardLayout();
}
}
class Lis implements ActionListener {
public static void main(String args[]){ }
public void actionPerformed(ActionEvent e){
Object r=e.getSource();
if(r==bt1 || r==bt2) mycard1.next(c);
else if(r==bt3) mycard1.first(c);
}
}