Java Reference
In-Depth Information
tainer 's setLayout method takes an object that implements the LayoutManager interface
as an argument. There are basically three ways for you to arrange components in a GUI:
1. Absolute positioning: This provides the greatest level of control over a GUI's ap-
pearance. By setting a Container 's layout to null , you can specify the absolute
position of each GUI component with respect to the upper-left corner of the Con-
tainer by using Component methods setSize and setLocation or setBounds . If
you do this, you also must specify each GUI component's size. Programming a
GUI with absolute positioning can be tedious, unless you have an integrated de-
velopment environment (IDE) that can generate the code for you.
2. Layout managers: Using layout managers to position elements can be simpler and
faster than creating a GUI with absolute positioning, and makes your GUIs more
resizable, but you lose some control over the size and the precise positioning of
each component.
3. Visual programming in an IDE: IDEs provide tools that make it easy to create
GUIs. Each IDE typically provides a GUI design tool that allows you to drag and
drop GUI components from a tool box onto a design area. You can then position,
size and align GUI components as you like. The IDE generates the Java code that
creates the GUI. In addition, you can typically add event-handling code for a par-
ticular component by double-clicking the component. Some design tools also al-
low you to use the layout managers described in this chapter and in Chapter 22.
Look-and-Feel Observation 12.15
Most Java IDEs provide GUI design tools for visually designing a GUI; the tools then
write Java code that creates the GUI. Such tools often provide greater control over the size,
position and alignment of GUI components than do the built-in layout managers.
Look-and-Feel Observation 12.16
It's possible to set a Container 's layout to null , which indicates that no layout manager
should be used. In a Container without a layout manager, you must position and size the
components and take care that, on resize events, all components are repositioned as neces-
sary. A component's resize events can be processed by a ComponentListener .
Figure 12.38 summarizes the layout managers presented in this chapter. A couple of
additional layout managers are discussed in Chapter 22.
Layout manager
Description
Default for javax.swing.JPanel . Places components sequentially, left to
right , in the order they were added. It's also possible to specify the order
of the components by using the Container method add , which takes a
Component and an integer index position as arguments.
FlowLayout
Default for JFrame s (and other windows). Arranges the components
into five areas: NORTH , SOUTH , EAST , WEST and CENTER .
BorderLayout
Arranges the components into rows and columns.
GridLayout
Fig. 12.38 | Layout managers.
 
Search WWH ::




Custom Search