Java Reference
In-Depth Information
You may have noticed that Figures 8-13 and 8-14 both use the Metal look and feel but dif-
ferent themes. Prior to JDK 1.5, the Metal look and feel looked like Figure 8-14; however, with
JDK 1.5 Sun has improved the standard look and feel, making the Ocean theme shown in
Figure 8-13. To revert to the old theme, the following command line was used:
java -Dswing.metalTheme=steel MyFrame javax.swing.plaf.metal.MetalLookAndFeel
Caution The layout managers have been designed to perform the hard work of determining where com-
ponents should be placed relative to one another, as well as determining the size of containers. It is possible
to explicitly place components with the setLocation method, and set their size with the setSize method
(which we used in Listing 8-4); however, as shown in Figures 8-11 through 8-15, the size of components
varies depending on the look and feel used, so deliberately setting the size or location of a component or a
container can result in strange-looking GUIs. We strongly recommend that you leave the work of component
placement and container sizing to the layout managers.
When a different look-and-feel package is used, the interface adopts not only the look of
that particular platform but also the functionality of its interface widgets. For example, a drop-
down menu in the Motif look and feel is vastly different from the menu functionality under
the Windows or Metal look and feel. This is because Motif represents the interface look and
also the functionality of an X Window system.
The JLabel Component
Standard user interfaces have labels near any component that accepts input, providing the
user with a brief explanation of what the user input is for. An example of this might be having
the label “Surname” next to the text field that accepts the surname data—the label serves to
remind the user what sort of data is to be entered in the text field.
The Swing component that holds a label is the JLabel . A simple constructor for this
could be
JLabel zipCodeLabel = new JLabel("Zip code");
While this on its own is not very exciting, you can also specify which character will be
displayed as the mnemonic (which character will be underlined). Since it does not make sense
for a label to have focus, you normally set the displayed mnemonic, and at the same time set
which field will get focus if the mnemonic is pressed. This could look similar to
zipCodeLabel.setDisplayedMnemonic('Z');
zipCodeLabel.setLabelFor(zipCode);
In this case, if a user presses the mnemonic key (by pressing the Alt and Z keys simultane-
ously), then focus will be transferred to the zipCode field.
This technique will be demonstrated in Listing 8-5 in the next section, with a sample GUI
displayed in Figure 8-16.
Search WWH ::




Custom Search