java事件机制
来源:互联网 发布:淘宝买的东西不合适 编辑:程序博客网 时间:2024/06/03 19:00
项目中的一个模块中使用了java属性事件机制,最近也在学习中。主要不是学习如何应用了,往深一层学习研究一下。
java的事件(非属性事件),我觉得主要关注事件的有和无,如按下一个按钮,就产生一个事件。主要应用在GUI中,而在AWT,SWING中已经定义好很多事件,我们只要实现就可以了。而在非GUI中,使用事件机制感觉没什么太大意义。如监控一个系统,当系统发生一个故障,被监控到,这就产生一个事件,如果按时java的机制,就要定义事件监听接口,实现接口,定义事件等好多步骤,实际上如下实现还比较直观:
if (故障产生)
{
processEorror();
}
没有再继续研究了,不过也自己试着写一个例子,就发送和接收邮件事件,没什么高明之处。
事件监听接口:
package cn.com.cathay.event;
import java.util.EventListener;
public interface MailListener extends EventListener
{
public void doMailEvent(MailEvent event);
}
//事件对象
package cn.com.cathay.event;
import java.util.EventObject;
public class MailEvent extends EventObject
{
/**
* receive and send
*/
private String action = null;
public MailEvent(Object source, String action)
{
super(source);
this.action = action;
}
public String getAction()
{
return this.action;
}
public void setAction(String action)
{
this.action = action;
}
}
//实现监听接口
package cn.com.cathay.event;
public class DoMailEvent implements MailListener
{
public void doMailEvent(MailEvent event)
{
if (event == null || event.getAction() == null)
{
return;
}
if ("send".equalsIgnoreCase(event.getAction()))
{
System.out.println("send mail >>>");
}
if ("receive".equalsIgnoreCase(event.getAction()))
{
System.out.println("receive mail <<<");
}
}
}
//事件源,如一个Button。包括测试代码。
package cn.com.cathay.event;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
public class MailOperator
{
protected Collection<MailListener> listenerList = null;
//增加事件
public void addMailListener(MailListener listener)
{
if (listenerList == null)
{
listenerList = new HashSet<MailListener>();
}
listenerList.add(listener);
}
//移除事件
public void removeMailListener(MailListener listener)
{
if (listenerList == null)
{
return;
}
else
{
listenerList.remove(listener);
}
}
//触发发送邮件事件
public void fireOnSendMail()
{
if (listenerList == null)
{
return;
}
else
{
MailEvent event = new MailEvent("MailOperator", "send");
doMail(event);
}
}
//触发收取邮件事件
public void fireOnReceiveMail()
{
if (listenerList == null)
{
return;
}
else
{
MailEvent event = new MailEvent("MailOperator", "receive");
doMail(event);
}
}
//处理邮件事件
protected void doMail(MailEvent event)
{
Iterator<MailListener> it = listenerList.iterator();
while (it.hasNext())
{
MailListener listerner = it.next();
listerner.doMailEvent(event);
}
}
//for test
public static void main(String[] args)
{
MailOperator mail = new MailOperator();
mail.addMailListener(new DoMailEvent());//注册事件
mail.fireOnReceiveMail();
mail.fireOnSendMail();
}
}
java的事件(非属性事件),我觉得主要关注事件的有和无,如按下一个按钮,就产生一个事件。主要应用在GUI中,而在AWT,SWING中已经定义好很多事件,我们只要实现就可以了。而在非GUI中,使用事件机制感觉没什么太大意义。如监控一个系统,当系统发生一个故障,被监控到,这就产生一个事件,如果按时java的机制,就要定义事件监听接口,实现接口,定义事件等好多步骤,实际上如下实现还比较直观:
if (故障产生)
{
processEorror();
}
没有再继续研究了,不过也自己试着写一个例子,就发送和接收邮件事件,没什么高明之处。
事件监听接口:
package cn.com.cathay.event;
imp
public interface MailListener extends EventListener
{
public void doMailEvent(MailEvent event);
}
//事件对象
package cn.com.cathay.event;
imp
public class MailEvent extends EventObject
{
/**
* receive and send
*/
private String act
public MailEvent(Object source, String act
{
super(source);
this.act
}
public String getAction()
{
return this.act
}
public void setAction(String act
{
this.act
}
}
//实现监听接口
package cn.com.cathay.event;
public class DoMailEvent implements MailListener
{
public void doMailEvent(MailEvent event)
{
if (event == null || event.getAction() == null)
{
return;
}
if ("send".equalsIgnoreCase(event.getAction()))
{
System.out.println("send mail >>>");
}
if ("receive".equalsIgnoreCase(event.getAction()))
{
System.out.println("receive mail <<<");
}
}
}
//事件源,如一个Button。包括测试代码。
package cn.com.cathay.event;
imp
imp
imp
public class MailOperator
{
protected Collection<MailListener> listenerList = null;
//增加事件
public void addMailListener(MailListener listener)
{
if (listenerList == null)
{
listenerList = new HashSet<MailListener>();
}
listenerList.add(listener);
}
//移除事件
public void removeMailListener(MailListener listener)
{
if (listenerList == null)
{
return;
}
else
{
listenerList.remove(listener);
}
}
//触发发送邮件事件
public void fireOnSendMail()
{
if (listenerList == null)
{
return;
}
else
{
MailEvent event = new MailEvent("MailOperator", "send");
doMail(event);
}
}
//触发收取邮件事件
public void fireOnReceiveMail()
{
if (listenerList == null)
{
return;
}
else
{
MailEvent event = new MailEvent("MailOperator", "receive");
doMail(event);
}
}
//处理邮件事件
protected void doMail(MailEvent event)
{
Iterator<MailListener> it = listenerList.iterator();
while (it.hasNext())
{
MailListener listerner = it.next();
listerner.doMailEvent(event);
}
}
//for test
public static void main(String[] args)
{
MailOperator mail = new MailOperator();
mail.addMailListener(new DoMailEvent());//注册事件
mail.fireOnReceiveMail();
mail.fireOnSendMail();
}
}
0 0
- Java事件机制---自定义事件
- Java事件机制---自定义事件
- java中的事件机制
- java 事件处理机制
- java事件机制
- JAVA事件处理机制
- JAVA事件处理机制
- java事件处理机制
- Java事件处理机制
- java事件处理机制
- java事件机制收藏
- java事件机制
- Java事件机制
- java事件监听机制
- java事件机制
- Java- 事件处理机制
- Java事件机制整理
- JAVA事件响应机制
- shell脚本:自动FTP和TELNET
- S9T9对于实时采集数据的项目
- unix shell获取IP和修改IP
- java使用SSH执行UNIX命令问题
- 关于debug使用及debug性能报告
- java事件机制
- java属性变化事件机制(1)
- java属性变化事件机制(2)
- 讲设计
- hadoop搭建与eclipse开发环境设置
- java hadoop分布式系统文件操作
- 用extjs 4.0打造自己的WEB桌面之一
- 用extjs 4.0打造自己的WEB桌面之二
- iOS调整音量+音量变化通知