Java Reference
In-Depth Information
Self-Test Exercises
(continued)
16. How would you modify the class definition in Display 17.7 so that the three
labels are displayed as follows?
First label
Second label
Third label
(There may be space between each pair of lines.)
17. Suppose you are defining a windowing GUI class in the usual way, as a derived
class of the class
JFrame
, and suppose you want to specify a layout manager for
the
JFrame
so as to produce the following sort of layout (that is, a one-row lay-
out, typically having three columns):
What should the argument to
setLayout
be?
18. Suppose the situation is as described in Self-Test Exercise 17, except that you
want the following sort of layout (that is, a one-column layout, typically having
three rows):
What should the argument to
setLayout
be?
Panels
A GUI is often organized in a hierarchical fashion, with window-like containers,
known as
panels,
inside of other window-like containers. A
panel
is an object of the
class
JPanel
, which is a very simple container class that does little more than group
objects. It is one of the simplest container classes, but an extremely useful one. A
JPanel
object is analogous to the braces used to combine a number of simpler Java
statements into a single larger Java statement. It groups smaller objects, such as but-
tons and labels, into a larger component (the
JPanel
). You can then put the
JPanel
object in a
JFrame
. Thus, one of the main functions of
JPanel
objects is to subdivide a
JFrame
(or other container) into different areas.
For example, when you use a
BorderLayout
manager, you can place components
in each of the five locations
BorderLayout.NORTH
,
BorderLayout.SOUTH
,
Border-
Layout.EAST
,
BorderLayout.WEST
, and
BorderLayout.CENTER
.
But what if you want to
put two components at the bottom of the screen in the
BorderLayout.SOUTH
position?
panel