计算机-外文翻译-外文文献-英文文献-事X件处理基础.doc
《计算机-外文翻译-外文文献-英文文献-事X件处理基础.doc》由会员分享,可在线阅读,更多相关《计算机-外文翻译-外文文献-英文文献-事X件处理基础.doc(19页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、 外文原文Basics of Event Handling出处:Thinking in java作者: Bruce Eckel Any operating environment that supports GUIs constantly monitors events such as keystrokes or mouse clicks. The operating environment reports these events to the programs that are running. Each program then decides what, if anything, to
2、 do in response to these events. In languages like Visual Basic, the correspondence between events and code is obvious. One writes code for each specific event of interest and places the code in what is usually called an event procedure. For example, a Visual Basic button named HelpButton would have
3、 a Help Button_Click event procedure associated with it. The code in this procedure executes whenever that button is clicked. Each Visual Basic GUI component responds to a fixed set of events, and it is impossible to change the events to which a Visual Basic component responds.On the other hand, if
4、you use a language like raw C to do event-driven programming, you need to write the code that constantly checks the event queue for what the operating environment is reporting. This technique is obviously rather ugly, and, in any case, it is much more difficult to code. The advantage is that the eve
5、nts you can respond to are not as limited as in languages, like Visual Basic, that go to great lengths to hide the event queue from the programmer.The Java programming environment takes an approach somewhat between the Visual Basic approach and the raw C approach in terms of power and, therefore, in
6、 resulting complexity. Within the limits of the events that the AWT knows about, you completely control how events are transmitted from the event sources (such as buttons or scrollbars) to event listeners. You can designate any object to be an event listenerin practice, you pick an object that can c
7、onveniently carry out the desired response to the event. This event delegation model gives you much more flexibility than is possible with Visual Basic, in which the listener is predetermined, but it requires more code and is more difficult to untangle (at least until you get used to it).Event sourc
8、es have methods that allow you to register event listeners with them. When an event happens to the source, the source sends a notification of that event to all the listener objects that were registered for that event.As one would expect in an object-oriented language like Java, the information about
9、 the event is encapsulated in an event object. In Java, all event objects ultimately derive from the class java.util.EventObject. Of course, there are subclasses for each event type, such as ActionEvent and WindowEvent.Different event sources can produce different kinds of events. For example, a but
10、ton can send ActionEvent objects, whereas a window can send WindowEvent objects.To sum up, heres an overview of how event handling in the AWT works. A listener object is an instance of a class that implements a special interface called (naturally enough) a listener interface. An event source is an o
11、bject that can register listener objects and send them event objects. The event source sends out event objects to all registered listeners when that event occurs. The listener objects will then use the information in the event object to determine their reaction to the event.You register the listener
12、 object with the source object by using lines of code that follow the modeleventSourceObject.addEventListener(eventListenerObject);Here is an example:ActionListener listener = . . .;JButton button = new JButton(Ok);button.addActionListener(listener);Now the listener object is notified whenever an ac
13、tion event occurs in the button. For buttons, as you might expect, an action event is a button click.Code like the above requires that the class to which the listener object belongs implements the appropriate interface (which in this case is the ActionListener interface). As with all interfaces in J
14、ava, implementing an interface means supplying methods with the right signatures. To implement the ActionListener interface, the listener class must have a method called actionPerformed that receives an ActionEvent object as a parameter.class MyListener implements ActionListener . . . public void ac
15、tionPerformed(ActionEvent event) / reaction to button click goes here . . . Whenever the user clicks the button, the JButton object creates an ActionEvent object and calls listener.actionPerformed(event), passing that event object. It is possible for multiple objects to be added as listeners to an e
16、vent source such as a button. In that case, the button calls the actionPerformed methods of all listeners whenever the user clicks the button.Figure 8-1 shows the interaction between the event source, event listener, and event object.Figure 8-1. Event notificationExample: Handling a Button ClickAs a
17、 way of getting comfortable with the event delegation model, lets work through all details needed for the simple example of responding to a button click. For this example, we will want A panel populated with three buttons; and Three listener objects that are added as action listeners to the buttons.
18、With this scenario, each time a user clicks on any of the buttons on the panel, the associated listener object then receives an ActionEvent that indicates a button click. In our sample program, the listener object will then change the background color of the panel.Before we can show you the program
19、that listens to button clicks, we first need to explain how to create buttons and how to add them to a panel. (For more on GUI elements, see Chapter 9.)You create a button by specifying a label string, an icon, or both in the button constructor. Here are two examples:JButton yellowButton = new JButt
20、on(Yellow);JButton blueButton = new JButton(new ImageIcon(blue-ball.gif);Adding buttons to a panel occurs through a call to a method named (quite mnemonically) add. The add method takes as a parameter the specific component to be added to the container. For example,class ButtonPanel extends JPanel p
21、ublic ButtonPanel() JButton yellowButton = new JButton(Yellow); JButton blueButton = new JButton(Blue); JButton redButton = new JButton(Red); add(yellowButton); add(blueButton); add(redButton); Figure 8-2 shows the result.Figure 8-2. A panel filled with buttonsNow that you know how to add buttons to
22、 a panel, youll need to add code that lets the panel listen to these buttons. This requires classes that implement the ActionListener interface, which, as we just mentioned, has one method: actionPerformed, whose signature looks like this:public void actionPerformed(ActionEvent event)NOTE: The Actio
23、nListener interface we used in the button example is not restricted to button clicks. It is used in many separate situations: When an item is selected from a list box with a double click; When a menu item is selected; When the ENTER key is clicked in a text field; When a certain amount of time has e
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 外文 翻译 文献 英文 处理 基础
限制150内