AWT_事件监听(Java)
package Demo01 ;
import java. awt. * ;
import java. awt. event. ActionEvent ;
import java. awt. event. ActionListener ;
import java. awt. event. WindowEvent ;
import java. awt. event. WindowListener ;
public class TestActionEvent {
public static void main ( String [ ] args) {
Frame frame= new Frame ( ) ;
Button button = new Button ( ) ;
MyActionListener myActionListener = new MyActionListener ( ) ;
button. addActionListener ( myActionListener) ;
frame. add ( button, BorderLayout . CENTER) ;
frame. pack ( ) ;
frame. setVisible ( true ) ;
WindownClose ( frame) ;
}
private static void WindownClose ( Frame frame) {
frame. addWindowListener ( new WindowListener ( ) {
@Override
public void windowOpened ( WindowEvent e) {
}
@Override
public void windowClosing ( WindowEvent e) {
System . exit ( 0 ) ;
}
@Override
public void windowClosed ( WindowEvent e) {
}
@Override
public void windowIconified ( WindowEvent e) {
}
@Override
public void windowDeiconified ( WindowEvent e) {
}
@Override
public void windowActivated ( WindowEvent e) {
}
@Override
public void windowDeactivated ( WindowEvent e) {
}
} ) ;
}
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed ( ActionEvent e) {
System . out. println ( "你好呀!" ) ;
}
}