模拟Button监听

来源:互联网 发布:微信照片打印机软件 编辑:程序博客网 时间:2024/06/16 09:39
package com.zhijun.test;import java.util.ArrayList;import java.util.List;public class TestButton {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubButton 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();for(int i=0; i<actionListeners.size(); i++) {ActionListener l = actionListeners.get(i);l.actionPerformed(e);}}public void addActionListener(ActionListener l) {actionListeners.add(l);}}interface ActionListener {public void actionPerformed(ActionEvent e);}class MyActionListener implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubSystem.out.println("button pressed!");}}class ActionEvent {public long getWhen() {return System.currentTimeMillis();}}

原创粉丝点击