Java - The Event Model

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击人工智能教程

What is an event?

An event is a signal received by a program from the operating system as a result of some action taken by the user, or because something else has happened. Here are some examples:

  • The user clicks a mouse button.
  • The user presses a key on the keyboard.
  • The user closes a window.
  • Some data from the Internet arrives at one of the computer's "ports".

More generally, an event is a state change at an instant in time (Winston). The word "event" also refers to an instance of the EventObject class, or a subclass of that class. Thus, such an instance describes a state change at a particular instant.

Another kind of event is associated with variable-value changes.

What is the delegation event model?

The delegation event model defines standard and consistent mechanisms to generate and process events:

  • listener registers with a source to receive event notifications.
  • The source generates events and sends notifications of these events to one or more listeners (in fact, to all those listeners who have registered to receive them).
  • When a listener receives notification of an event, the listener then processes (or "handles") the event and returns.

Listener Classes

Key to understanding how the Java event model works is understanding how "listener classes" are used. A listener class may be defined by:

  • Implementing a listener interface
  • Extending another listener class

You must make sure that any listener class has appropriately defined listener methods, and that an instance of the listener class has been registered with the source of the event for which it is listening. The built-in machinery then notifies the listener object when the source object generates an event of the type for which the listener object is listening.

How to Implement an Event Handler (according to the Sun site)

Every event handler requires three pieces of code:

1. In the declaration for the event handler class, one line of code specifies that the class either implements a listener interface or extends a class that implements a listener interface. For example:

public class MyClass implements ActionListener {}

2. Another line of code registers an instance of the event handler class as a listener on one or more components. For example:

someComponent.addActionListener(instanceOfMyClass);

3. In the event handler class, a few lines of code implement the methods in the listener interface. For example:

public void actionPerformed(ActionEvent e)
{
    ... // code that reacts to the action...
}

Event handlers can be instances of any class. Often an event handler that has only a few lines of code is implemented using an anonymous inner class--an unnamed class defined inside of another class. Anonymous inner classes can be confusing at first, but once you're used to them, they make the code clearer by keeping the implementation of an event handler close to where the event handler is registered.

Miscellaneous Notes on Events

  • Although we usually introduce events in the context of the user interface and the events caused by a user interacting with the user interface, we should not forget that events are not limited to this context. In fact, events are an extremely important interobject communications mechanism, which may be used by completely non-graphical parts of an application as well.
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值