📄 13_1.htm
字号:
<HTML><HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <META NAME="Author" CONTENT="Weng Kai"> <META NAME="GENERATOR" CONTENT="Mozilla/4.01 [en] (Win95; I) [Netscape]"> <TITLE>The new event model</TITLE></HEAD><BODY><H2>13.1 The new event model</H2><HR WIDTH="100%"><BR>In the new event model a component can initiate ("fire") an event.Each type of event is represented by a distinct class. When an event isfired, it is received by one or more "listeners," which act on that event.Thus, the source of an event and the place where the event is handled canbe separate.<P>Each event listener is an object of a class that implements a particulartype of listener interface. So as a programmer, all you do is create alistener object and register it with the component that's firing the event.This registration is performed by calling a addXXXListener( ) method inthe event-firing component, in which XXX represents the type of event listenedfor. You can easily know what types of events can be handled by noticingthe names of the addListener methods, and if you try to listen for thewrong events you'll find out your mistake at compile time. Java Beans alsouses the names of the addListener methods to determine what a Bean cando.<P>All of your event logic, then, will go inside a listener class. Whenyou create a listener class, the sole restriction is that it must implementthe appropriate interface. You can create a global listener class, butthis is a situation in which inner classes tend to be quite useful, notonly because they provide a logical grouping of your listener classes insidethe UI or business logic classes they are serving, but because (as youshall see later) the fact that an inner class object keeps a handle toits parent object provides a nice way to call across class and subsystemboundaries.<P>A simple example will make this clear. Consider the Button2.java examplefrom earlier in last lesson.<P>Case Study: <A HREF="case/Button2New.java">Button2New.java</A><P>So you can compare the two approaches, the old code is left in as acomment. In init( ), the only change is the addition of the two lines:<UL><PRE>b1.addActionListener(new B1());</PRE><PRE>b2.addActionListener(new B2());</PRE></UL>addActionListener( ) tells a button which object to activate when the buttonis pressed. The classes B1 and B2 are inner classes that implement theinterface ActionListener. This interface contains a single method actionPerformed() (meaning "This is the action that will be performed when the event isfired"). Note that actionPerformed( ) does not take a generic event, butrather a specific type of event, ActionEvent. So you don't need to bothertesting and downcasting the argument if you want to extract specific ActionEventinformation.<P>One of the nicest things about actionPerformed( ) is how simple it is.It's just a method that gets called. Compare it to the old action( ) method,in which you must figure out what happened and act appropriately, and alsoworry about calling the base class version of action( ) and return a valueto indicate whether it's been handled. With the new event model you knowthat all the event-detection logic is taken care of so you don't have tofigure that out; you just say what happens and you're done. If you're don'talready prefer this approach over the old one, you will soon.<P><HR WIDTH="100%"><DIV ALIGN=right><A HREF="13_2.htm">Next Page</A></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -