|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
The OKDialog constructor instantiates a button and a label and adds them to the dialog, then calls pack() to shrink the dialog window down to a size that contains the components. The listener that is attached to the dialogs button makes the dialog window invisible.
Understanding theChoice In Java, a Choice is a pop-up list that displays one item at a time. We add a Choice (called theChoiceits a local variable, not an instance variable) and a matching Label. Then we add an ItemListener. When we get an itemStateChanged() event, we use getItemSelectable() to get a reference back to theChoice and then call getSelectedObjects() to get an array of selected items. If theChoice is working correctly, we should be able to get back only a single item. (We throw an exception if that condition doesnt hold.) Finally, we modify fLabelForChoice to reflect the users choice. Working with LayoutsRecall that several of the sample applets shown in Chapter 37 use layout managers such as CardLayout or GridLayout to manage the placement of components on the screen. You can see the effect of a layout manager by resizing the HelloPlus window. Figure 38.6 shows a maximized window in which the components have room to spread out; in Figure 38.7, the window is tall and narrowthe components appear one above the other.
You dont have to worry about the precise location of every component or whether the user is working on a 640 × 480 screen or an older Macintosh with a 9-inch monitor. Thats because every Java window has an associated LayoutManager. If you dont specify the manager, Java supplies one for you. This section walks you through the standard layouts:
Using FlowLayoutFigures 38.6 and 38.7 showed how HelloPlus.java looks when you use the default layout for Panel, FlowLayout. (Recall that an Applet is a kind of Panel.) If youre happy with this designall the components laid out left to right, top to bottom, with each component taking up whatever amount of space it requiresyou dont have to do anything. Just call add() to place each component into the Container. If youre working in a Window, where the default layout is BorderLayout, youll need to change the layout manager if you want a FlowLayout. Just write setLayout(new FlowLayout()); before you add() any components. Using GridLayoutFigure 38.8 shows how HelloPlus looks in a GridLayout. A GridLayout is similar to a FlowLayout, but each component gets the same size cell as all the others. The cell size is determined by the size of the largest component. To apply a GridLayout to HelloPlus, add this line to the top of init(): setLayout(new GridLayout(rows, columns)); where rows gives the number of rows and columns gives the number of columns. If you set either value to zero, Java will use as many of that dimension as necessary to display all the components in the layout.
Using BorderLayoutBorderLayout is particularly appropriate when you have one large component and several smaller ones because you can place the large component in the center position. Figure 38.9 illustrates the general design of BorderLayout, and Figure 38.10 shows HelloPlus.java in this layout.
To modify HelloPlus.java to use a BorderLayout, you need to make three changes:
|
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. |