|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Most of these components provide familiar functionality. A Button, for example, works the way you expect buttons to work in any GUI. The classes Canvas and Composite enable you to use drawing primitives to build a custom GUI component. Whats most significant about this list is that it includes java.awt.Container, which means you can build a new drawable area and add it to an existing Container. The AWT supplies a variety of subclasses of Container, including Panel, ScrollPane, and Window. Window, in turn, has two subclasses in the AWT: Dialog and Frame. (Each of those classes has further subclasses, but were not concerned about those just now.) To add a GUI to an application, you must supply a top-level Frame into which the application can add Components. Thats what were doing in line 18 of Listing 38.1: class HelloApplicationFrame extends Frame
Introducing the JDK 1.1 Delegation Event ModelMuch of the work of writing a user interface has to do with communication. If the operating system detects a key being pressed or a mouse button coming up, it needs to notify the right application. The application, in turn, needs to find out which components are interested in that activity and send them a message. Starting with JDK 1.1, Sun introduced a new way of communicating information about events: the delegation event model. Prior to JDK 1.1 the model was, well, different. Dont worry about how the user interface worked in JDK 1.0its gone for good. Do be aware that the mechanism did change; if you have occasion to read old code or you have old books on Java, dont copy the way they do things. Its use is discouraged in JDK 1.1 or later because it doesnt support some of the newest features (such as Javas component model, JavaBeans).
So how does the delegation event model work? Every element of communication between the GUI and the program is defined as an event. Application classes register their interest in particular events from particular components by asking the component to add their listener to a list. When the event occurs, the event source notifies all registered listeners. Two kinds of events exist: low-level events and semantic events. All of them are derived from java.awt.AWTEvent. A low-level event is concerned with the physical aspects of the user interfacemouse clicks, key presses, and the like. Semantic events are based on low-level events. To choose a menu item, for example, a user may click the menu bar, then click a menu item. To click means to press the mouse button down and then up again. This series of low-level events (mouse-down on the menu bar, mouse-up on the menu bar, mouse-down on the menu item, and mouse-up on the menu item) is combined into one semantic event. Class ComponentEvent includes a special type of event that is not used with the delegation event model: PaintEvent. A PaintEvent signals that the operating system wants to redraw a portion of the user interface. A component must override paint() or update() to make sure it handles the PaintEvent correctly. Look at line 23 of Listing 38.1: addWindowListener(new HelloWindowAdapter()); This line is part of HelloApplicationFrames constructor. As the new Frame is being built, this line tells the Frame that it is interested in certain events. Rather than notify it directly, however, the constructor tells it to send the notifications to an adaptera convenience class that handles only one kind of event. Our adapter is called HelloWindowAdapterits an instance of WindowAdapter, which is interested in WindowEvents. When the frame sends a WindowEvent, HelloWindowAdapter looks to see if the message is windowClosing. If it isnt, HelloWindowAdapter ignores it, but if it is, the program starts the process of shutting down. Because HelloApplication is written as both an applet and an application, it begins its shutdown by calling the methods a browser calls when it wants to shut down an appletstop() and destroy(). Finally it calls exit(), ending the application and enabling the Frame to close. Class AWTEvent also includes semantic events:
Drawing and addingConstructing the User InterfaceIn Listing 38.1 we implemented the applets paint method by calling drawString() on the Graphics object. The drawString() method is one of the drawing primitives that has been available in Java from its earliest days. Graphics drawing methods also include
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |