Java Reference
In-Depth Information
The PanelDemo class extends Frame and has four fields: three Button refer-
ences and a List reference. Notice in the PanelDemo constructor that the three
Button objects are placed in a Panel named southPanel, and southPanel is
added to the south of the Frame. Because the Buttons are in a Panel with
FlowLayout, they will get their preferred size. A Label is put on a Panel named
northPanel, and the northPanel is placed in the north of the Frame. Putting the
Label on a Panel allows it to be centered along the top of the window because
the FlowLayout manager of the northPanel centers its components.
A List is added to the center of the Frame to demonstrate that the remaining
space in the Frame is filled with whatever component is placed there. The List
is not placed on a Panel, but is simply added directly to the Frame. If the win-
dow in Figure 12.7 is resized, the Buttons and Label will be recentered, and the
List will be resized to fill the center region of the Frame.
After I discuss the GridLayout and BoxLayout managers, I will show you an
example using JPanel that nests panels within each other.
GridLayout Manager
The java.awt.GridLayout class represents a layout manager that divides a con-
tainer into a grid of rows and columns. The GridLayout manager has the fol-
lowing properties:
Only one component can be added to each region of the grid. (Of
course, that one component can be a panel containing any number of
components.)
■■
Each region of the grid will have the same size. When the container is
resized, each region of the grid will be resized accordingly.
■■
No components get their preferred height or width. Each component in
the container is the same size, which is the current size of the regions of
the grid.
■■
The order in which components are added determines their locations
in the grid. The first component added appears in the first row and col-
umn, and subsequent components fill in the columns across the first
row until that row is filled. Then, the second row is filled, and then the
third, and so on.
■■
To instantiate a GridLayout object, you use one of its three constructors:
public GridLayout(int rows, int cols). Creates a new GridLayout with
the specified number of rows and columns. The horizontal and vertical
gap between components is five units.
Search WWH ::




Custom Search