研究了一下swing,对按钮监听事件好奇,于是自己做了一个监听的最简单案例
1,接口
package com.cxc;
public interface ReturnListener {
public void success(String str);
public void failed(String str);
}
2,执行类
<pre name="code" class="java">package com.cxc;
public class Post {
private ReturnListener mReturnListener;
private String method;
public Post( String method,ReturnListener mReturnListener) {
super();
this.mReturnListener = mReturnListener;
this.method = method;
}
public void run(){
if (method.equals("cxc")) {
mReturnListener.failed(method+",调用失败!");
}else{
mReturnListener.success(method+",调用成功!");
}
}
}
3,调用
package com.cxc;
public class Main {
public static void main(String[] args) {
Post post = new Post("cqxc", new ReturnListener() {
@Override
public void success(String str) {
System.out.println(str);
}
@Override
public void failed(String str) {
System.out.println(str);
}
});
post.run();
}
}
参数为”cxc“,控制台打印:cxc,调用失败!
参数为其他:控制台打印:cxc,调用成功!