Java Reference
In-Depth Information
Figure 8-12 on the previous page also displays the constructors to create the
JLabel, JTextField, and JButton components. In lines 35, 39, and 45, extra spaces
are included in the JLabel() method arguments to cause the length of the label
prompt to align with the displayed JTextField that is constructed in the next line.
Manipulating components in this manner prevents programmers from coding
absolute placement or further complicating the coding with multiple varieties of
subsetted layout managers.
The following step enters the constructor method code.
To Construct the Components
1. Enter the code from Figure 8-12 on the previous page, lines 17 through
54.
The TextPad window displays the constructor code (Figure 8-13).
output stream
declared
JPanels
constructed
JLabels and JTextFields
constructed
JButton
constructed
FIGURE 8-13
Although the application contains only one button, placing it in a panel
allows it to retain its normal size rather than stretching it across the interface.
Setting the Layout Managers
The next step is to establish the placement of the components in the interface.
Because you are using a Swing application, a content pane is necessary. The code in
Chapter 7 created a separate createContentPane() method. It is permissible, how-
ever, to assign the container in the constructor method for the entire class, espe-
cially if no extenuating tabs, styles, or scroll bars exist. The content pane contains
two panels. The fieldPanel and buttonPanel are placed using the BorderLayout
manager, and the subsetted row panels use the FlowLayout manager.
 
Search WWH ::




Custom Search