Java Reference
In-Depth Information
2.
Run the project from Eclipse. You should see the window in Figure 11-18.
figure 11-18
3.
Observe what happens when you resize this window (there is no layout manager now to stretch
things around). Also, try changing the order of the
add
method lines to make the green panel
appear above the other ones (it is currently hidden).
How It Works
This is how it works:
1.
The workings of this class should be easy to understand. The main things to note are the use of a
null
layout manager, as well as calling
setBounds
on the panels. Four dimensions are given: x‐
position, y‐position, width, and height.
2.
Finally, note the use of the
setSize
method here to size the
JFrame
, as there is now no layout
manager that can make an informed guess based on the components contained in the
JFrame
con-
tainer on which size to give the frame.
This concludes the overview on layout managers and how to use them. Take your time to play
around with them and create some nested structures. Before moving on to the next big topic when
dealing with GUIs—events—consider these helpful tips that help you when you lay out components.
➤
One of the problems beginners encounter when dealing with layout managers relates to the
sizing of components. Remember that
setSize
and
setBounds
only work when a layout
Search WWH ::
Custom Search