Java Reference
In-Depth Information
Glue
The for statement at lines 38-42 adds three JButton s to horizontal2 . Before adding each
button, line 40 adds horizontal glue to the container with static Box method create-
HorizontalGlue . Horizontal glue is an invisible GUI component that can be used be-
tween fixed-size GUI components to occupy additional space. Normally, extra space
appears to the right of the last horizontal GUI component or below the last vertical one in
a BoxLayout . Glue allows the extra space to be placed between GUI components. When
the container is resized, components separated by glue components remain the same size,
but the glue stretches or contracts to occupy the space between them. Class Box also de-
clares method createVerticalGlue for vertical BoxLayout s.
Rigid Areas
The for statement at lines 45-49 adds three JButton s to vertical2 . Before each button
is added, line 47 adds a rigid area to the container with static Box method create-
RigidArea . A rigid area is an invisible GUI component that always has a fixed pixel width
and height. The argument to method createRigidArea is a Dimension object that speci-
fies the area's width and height.
Setting a BoxLayout for a Container
Lines 52-53 create a JPanel object and set its layout to a BoxLayout in the conventional
manner, using Container method setLayout . The BoxLayout constructor receives a ref-
erence to the container for which it controls the layout and a constant indicating whether
the layout is horizontal ( BoxLayout.X_AXIS ) or vertical ( BoxLayout.Y_AXIS ).
Adding Glue and JButton s
The for statement at lines 55-59 adds three JButton s to panel . Before adding each but-
ton, line 57 adds a glue component to the container with static Box method create-
Glue . This component expands or contracts based on the size of the Box .
Creating the JTabbedPane
Lines 62-63 create a JTabbedPane to display the five containers in this program. The ar-
gument JTabbedPane.TOP sent to the constructor indicates that the tabs should appear at
the top of the JTabbedPane . The argument JTabbedPane.SCROLL_TAB_LAYOUT specifies
that the tabs should wrap to a new line if there are too many to fit on one line.
Attaching the Box Containers and JPanel to the JTabbedPane
The Box containers and the JPanel are attached to the JTabbedPane at lines 66-70. Try
executing the application. When the window appears, resize the window to see how the
glue components, strut components and rigid area affect the layout on each tab.
22.10 GridBagLayout Layout Manager
One of the most powerful predefined layout managers is GridBagLayout (in package ja-
va.awt ). This layout is similar to GridLayout in that it arranges components in a grid, but
it's more flexible. The components can vary in size (i.e., they can occupy multiple rows
and columns) and can be added in any order.
The first step in using GridBagLayout is determining the appearance of the GUI. For
this step you need only a piece of paper. Draw the GUI, then draw a grid over it, dividing
 
 
Search WWH ::




Custom Search