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 ( ) ;
Button button= new Button ( "" ) ;
MyActionListener myActionListener= new MyActionListener ( ) ;
button. addActionListener ( myActionListener) ;
frame. add ( button, BorderLayout. CENTER) ;
frame. pack ( ) ;
frame. setVisible ( true ) ;
windowClose ( frame) ;
}
private static void windowClose ( Frame frame) {
frame. addWindowListener ( new WindowAdapter ( ) {
@Override
public void windowClosing ( WindowEvent e) {
System. exit ( 0 ) ;
}
} ) ;
}
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed ( ActionEvent e) {
System. out. println ( "aaa" ) ;
}
}
import java. awt. *;
import java. awt. event. ActionEvent;
import java. awt. event. ActionListener;
public class TestActionEvent02 {
public static void main ( String[ ] args) {
Frame frame= new Frame ( "开始-停止" ) ;
Button button1= new Button ( "Start" ) ;
Button button2= new Button ( "Stop" ) ;
button2. setActionCommand ( "button2-stop" ) ;
MyMonitor myMonitor= new MyMonitor ( ) ;
button1. addActionListener ( myMonitor) ;
button2. addActionListener ( myMonitor) ;
frame. add ( button1, BorderLayout. WEST) ;
frame. add ( button2, BorderLayout. EAST) ;
frame. pack ( ) ;
frame. setVisible ( true ) ;
}
}
class MyMonitor implements ActionListener {
@Override
public void actionPerformed ( ActionEvent e) {
System. out. println ( "按钮被点击了:msg-> " + e. getActionCommand ( ) ) ;
if ( e. getActionCommand ( ) . equals ( "Start" ) ) {
}
}
}