Java Reference
In-Depth Information
With a
BorderLayout
, if you put something in the
NORTH
section, it will
appear across the top area of that container. If you resize the container (e.g.,
drag the window edges), it will take extra space and use it for horizontal, but
not vertical, stretching. That is, the objects won't get bigger than they need to
vertically, though they will stretch wider. The same is true for
SOUTH
, but the
objects are at the bottom rather than top of the container. Putting something
in
EAST
or
WEST
will move them to the left or right of the container. For these
two areas, though, space when resizing a window is added to the objects verti-
cally, but not horizontally. Putting an object in
EAST
or
WEST
will let it get
taller, but not wider.
The
CENTER
area, the default location if you use the
add()
method with
no second parameter, will use extra space both vertically and horizontally.
Adding more than one object into a region (e.g.,
NORTH
) will result in only
the last item added being displayed. For this reason, too, one often builds inter-
mediate containers to hold several objects. Then the single container object is
added to one of
BorderLayout
's regions.
16.7.2.1
Let's look at the simplest of the three pieces that we create for our GUIāthe
top line of information indicating the status of the account. In lines 88-107
we create this portion of the GUI.
Simple
JLabel
s
88 private Component
89 createStatus()
90 {
91 JPanel retval = new JPanel(); // default: flow layout
92
93 upton.addActionListener(upAction);
94
95 nam = new JLabel("Account: Name");
96 tot = new JLabel("Total: $");
97 val = new JLabel("Remaining: $");
98
99 retval.add(upton);
100 retval.add(nam);
101 retval.add(tot);
102 retval.add(val);
103
104 setStatus();
105
106 return retval;
107 } // createStatus