Java Reference
In-Depth Information
assigning a null layout manager to your container and specifying
the exact location and size of each component added. There is an
example of this later in the chapter in the section Using No Layout
Manager .
Q: So why would you ever use one of the layout managers? Why not
just use the IDE or lay out the components exactly where you
want them?
A: Two reasons: First, you might not have an IDE, and if you do, there
is a certain complexity to figuring out how to use it. If you under-
stand layout managers, this will help you comprehend the code
that the IDE is generating for you. Second, using a layout manager
to lay out your components makes your GUI more portable. You
might be surprised to see that a GUI that you created using an IDE
looks great on Windows, but not so great on a Unix or Macintosh
platform. The same problem can occur if you try to lay out com-
ponents exactly where you want them.
Q: How does the layout manager know how you want your GUI to
look?
A: Good question. You need to understand the way each type of lay-
out manager behaves. For example, you need to know that the
FlowLayout manager gives components their preferred size, and
that BorderLayout places components in specific regions of the
container. By using the different layout managers and nesting con-
tainers, you have great control over the look of the GUI, while at
the same time letting the layout managers determine the exact
location and size of your GUI components. Let's look at some of
these layout managers so you can get a feel for how they are used.
Layout Managers
A container uses a layout manager to determine both the location and size of
the components within the container. A container can be assigned one lay-
out manager, which is done using the setLayout() method of the java.awt
.Container class:
public void setLayout(LayoutManager m)
LayoutManager is an interface that all the layout managers' classes must
implement. You can create your own layout manager by writing a class that
implements the methods of the LayoutManager interface (no small task), or
Search WWH ::




Custom Search