点击
package cn.usts.edu.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestActionEvent {
public static void main(String[] args) {
Frame frame = new Frame("action Event");
Button button = new Button();
.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame
// 按钮监听
= new MyActionListener();
MyActionListener myActionListener // addActionListener需要一个ActionListener于是我们创建了一个myActionListener
.addActionListener(myActionListener);
button
// 关闭
myClose(frame);
}
// 监听关闭按钮
private static void myClose(Frame frame){
.addWindowListener(new WindowAdapter() {
frame@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("监听到按钮动作");
}
}