Java Reference
In-Depth Information
The panel of buttons along the top edge of the interface is stacked horizontally. If the
second argument to the constructor in lines 11-12 was BoxLayout.Y_AXIS , the buttons
would be arranged vertically instead.
Grid Layout
The grid layout manager arranges components into a grid of rows and columns.
Components are added first to the top row of the grid, beginning with the leftmost grid
cell and continuing to the right. When all the cells in the top row are full, the next com-
ponent is added to the leftmost cell in the second row of the grid—if there is a second
row—and so on.
Grid layout managers are created with the GridLayout class, which belongs to the
java.awt package. Two arguments are sent to the GridLayout constructor—the number
of rows in the grid and the number of columns. The following statement creates a grid
layout manager with 10 rows and 3 columns:
GridLayout gr = new GridLayout(10, 3);
As with flow layout, you can specify a vertical and a horizontal gap between components
with two extra arguments. The following statement creates a grid layout with 10 rows
and 3 columns, a horizontal gap of 5 pixels, and a vertical gap of 8 pixels:
11
GridLayout gr2 = new GridLayout(10, 3, 5, 8);
The default gap between components under a grid layout is 0 pixels in both vertical and
horizontal directions.
Listing 11.3 contains an application that creates a grid with three rows, three columns,
and a 10-pixel gap between components in both the vertical and horizontal directions.
LISTING 11.3
The Full Text of Bunch.java
1: import java.awt.*;
2: import java.awt.event.*;
3: import javax.swing.*;
4:
5: public class Bunch extends JFrame {
6: JButton marcia = new JButton(“Marcia”);
7: JButton carol = new JButton(“Carol”);
8: JButton greg = new JButton(“Greg”);
9: JButton jan = new JButton(“Jan”);
10: JButton alice = new JButton(“Alice”);
11: JButton peter = new JButton(“Peter”);
12: JButton cindy = new JButton(“Cindy”);
13: JButton mike = new JButton(“Mike”);
14: JButton bobby = new JButton(“Bobby”);
15:
Search WWH ::




Custom Search