import java.awt.*;
import javax.swing.*;
public class ActionEventDemo {
public static void main(String args[]) {
JFrame frame = new JFrame ("ActionEvent Demo");
JButton b = new JButton("Press me");
//注册事件监听程序
b.addActionListener(new ButtonHandler());
frame.getContentPane().add(b,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
//下面是ButtonHandler类的定义:
import java.awt.event.*;
public class ButtonHandler implements ActionListener {
//出现ActionEvent事件时,下面方法将被调用
public void actionPerformed(ActionEvent e) {
System.out.println("Action occurred");
}
}