Java Reference
In-Depth Information
Figure 12.3
The Button is the same size as the Frame.
The button shown in Figure 12.3 looks a little unusual. I haven't seen many
GUI programs that are one large button.
Classroom Q & A
Q: OK, I'll ask the obvious question here. Why is the Button the same
size as the Frame?
A: The answer involves the concept of a layout manager. A container
uses a layout manager to determine how components are laid out
within the container. The Frame class uses a BorderLayout man-
ager by default, and the BorderLayout manager has placed the
Button in the center of the Frame.
Q: But that is an unusual layout. What if we want the Button to have
a normal size and appear in a certain location?
A: In the AddDemo example, I could have given the button a normal
size by using a different layout manager such as FlowLayout. How-
ever, with layout managers, you do not specify the exact size and
location of a GUI component. Instead, you simply add components
to your container, and the layout manager determines where each
component will go and what its size will be .
Q: That sounds odd. How does the layout manager know what you
want the GUI to look like?
A: You determine the look of the GUI by selecting the appropriate
layout manager and giving the components to the layout manager
in a specific order or with specific constraints.
Q: I have done some GUI programming using Visual Basic, and all I
did was visually place the components exactly where I wanted
them in the window using the Visual Basic IDE. Can you do that in
Java?
A: You can if you have an IDE like Visual Café or Visual Age. These
IDEs have GUI editors that let you place components exactly
where you want them. You can also organize components by
Search WWH ::




Custom Search