package _09_awt;
import java.util.ArrayList;
import java.util.List;
public class Test {
/**
* @param args
* 模仿 AWT
*/
public static void main(String[] args) {
Button b=new Button();
b.addActionListener(new MyActionListener());
b.buttonPressed();//模拟按下按钮
}
}
class Button{
private List<ActionListener> actionListeners=new ArrayList<ActionListener>();
public void buttonPressed() {
ActionEvent e=new ActionEvent(System.currentTimeMillis(),this);
for(ActionListener actionListener:actionListeners){
actionListener.actionPerformed(e);
}
}
public void addActionListener(ActionListener l){
actionListeners.add(l);
}
}
interface ActionListener{
public void actionPerformed(ActionEvent e);
}
class ActionEvent{
long when;
Object source;
public ActionEvent(long when,Object source){
this.when=when;
this.source=source;
}
public long getWhen(){
return when;
}
public Object getSource(){
return source;
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("button pressed");
System.out.println("time:"+e.getWhen());
System.out.println("source:"+e.getSource());
}
}