Java Reference
In-Depth Information
Basic Interface Layout
As you learned yesterday, a graphical user interface designed with Swing is a fluid thing.
Resizing a window can wreak havoc on your interface, as components move to places on
a container that you might not have intended.
This fluidity is a necessary part of Java's support for different platforms where there are
subtle differences in the way each platform displays things such as buttons, scrollbars,
and so on.
With programming languages such as Microsoft Visual Basic, a component's location on
a window is precisely defined by its x,y coordinate. Some Java development tools allow
similar control over an interface through the use of their own windowing classes (and
there's a way to do that in Java).
When using Swing, a programmer gains more control over the layout of an interface by
using layout managers.
Laying Out an Interface
A layout manager determines how components will be arranged when they are added to
a container.
The default layout manager for panels is the FlowLayout class. This class lets compo-
nents flow from left to right in the order that they are added to a container. When there's
no more room, a new row of components begins immediately below the first, and the
left-to-right order continues.
Java includes a bunch of general-purpose layout managers: BorderLayout , BoxLayout ,
CardLayout , FlowLayout , GridBagLayout , and GridLayout . To create a layout manager
for a container, first call its constructor to create an instance of the class, as in this
example:
FlowLayout flo = new FlowLayout();
After you create a layout manager, you make it the layout manager for a container by
using the container's setLayout() method. The layout manager must be established
before any components are added to the container. If no layout manager is specified, its
default layout will be used— FlowLayout for panels and BorderLayout for frames and
windows.
The following statements represent the starting point for a frame that uses a layout man-
ager to control the arrangement of all the components that will be added to the frame:
 
Search WWH ::




Custom Search