package Other;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test extends JFrame{
private ButtonTest bt;
public Test(){
this.setTitle("JButton事件例子");
this.setBounds(200, 200, 400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setLayout(null);
bt = new ButtonTest(4);
bt.setText("点我啊啊啊");
bt.setBounds(100, 100, 100,40);
this.add(bt);
}
public static void main(String[] args) {
new Test();
}
}
/**
* 这个类继承了JButton类并且实现了ActionListener接口
* @author Administrator
*
* */
class ButtonTest extends JButton implements ActionListener{
private int num = 0;
public ButtonTest(){this.addActionListener(this);}
public ButtonTest(int num){
this.num = num;
this.addActionListener(this);
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(this.num++);
}
}