Java Reference
In-Depth Information
J ava 2
v 5 . 0
J ava 2
JAVA UPDATE
Lines 48 through 58 add components to the roomPanel. Line 48 starts a for
loop that begins at 1 and continues sequentially through 8, while the index
number is less than 9. With each pass of the loop, the TextArea() constructor in
line 50 takes four arguments: the default String to display (null means no default
text), the number of text rows to display (a 3 means three rows of text), approxi-
mate average character width that is platform-dependent (in this case 5), and a
number representing scroll bar visibility (a 3 means no scroll bars).
An if...else statement in lines 51 through 54 determines whether to add the
text, Nonsmoking or Smoking, to the TextArea component. In line 55, the
setEditable() method locks each TextArea component, which means the user
cannot enter text into the TextArea at run time. Line 56 uses the setBackground()
method to set the background of each TextArea component to light green.
In line 57, the TextArea component representing a room is added to the
roomPanel. As with applets, Java has an add() method for adding each type of
component to a container.
In line 61, the bookButton button is added to the buttonPanel. Lines 64
through 73 add the Labels, TextFields, Choice, and Checkbox components to
the inputPanel.
In lines 70 and 71, a for loop is used to populate , or insert data into, the
drop-down list of the Choice component. The population of a drop-down list in
a Choice component is similar to adding data to an array. As the data is entered,
it is given an index number that begins with zero and increases by one each time
new data is entered. The add() method is used to specify the data. Programmers
typically place multiple add() methods together in the code, directly after the
addition of the object itself. The add() method inserts data into a Choice com-
ponent, beginning at the top and adding a new line with each pass of the loop.
In the case of the Reservations class, the Choice component will display the
number of guests in a customer's party reservation, a number between 8 and 20.
The for loop's increment value, i, is used to provide the data argument for the
add() method. Because this data is an int, it must be converted to a String data
type required by the Choice's add() method. The valueOf() method returns a
String value from its int argument, effectively converting the int to a String.
Similar to the toString() method, Java supports a valueOf() method to create a
String representation of each primitive data type.
Lines 76 through 80 position each of the three Panels in a BorderLayout
region and add the ActionListener to the Button. Notice that you need not
include the object name in front of the add() method to add the Panels to the
Frame. When no object is specified, the add() method relates to the entire Frame.
v 5 . 0
A new feature of J2SE
version 5.0 is the inclu-
sion of static imports.
A static import is a way
for programmers to
simplify their code by
dropping reference to
the BorderLayout man-
ager when specifiying
the region.
For example, previously
the add() method
required programmers
to type:
add(label,
BorderLayout.SOUTH);
In version 5.0, pro-
grammers import the
static constants with
the code:
import static
java.awt.
BorderLayout.*;
Then when they get
ready to add a compo-
nent to a region, they
simply type:
add(label, SOUTH);
Static imports should
be used sparingly and,
typically, only for con-
stants.
If you wish to use static
imports in the Reserva-
tions program, insert
new code for line 13,
and replace lines 76,
77, and 78.
13
import static java.awt. BorderLayout .*;
76
add ( buttonPanel, SOUTH ) ;
77
add ( inputPanel, CENTER ) ;
78
add ( roomPanel, NORTH ) ;
79
Search WWH ::




Custom Search