Java Reference
In-Depth Information
789
Figure 1
Components Expand to Fill Space in the Border Layout
The content pane of a frame has a border layout by default. A panel has a flow
layout by default.
The grid layout is a third layout that is sometimes useful. The grid layout arranges
components in a grid with a fixed number of rows and columns, resizing each of the
components so that they all have the same size. Like the border layout, it also
expands each component to fill the entire allotted area. (If that is not desirable, you
need to place each component inside a panel.) Figure 2 shows a number pad panel
that uses a grid layout. To create a grid layout, you supply the number of rows and
columns in the constructor, then add the components, row by row, left to right:
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 3));
buttonPanel.add(button7);
buttonPanel.add(button8);
buttonPanel.add(button9);
buttonPanel.add(button4);
. . .
Sometimes you want to have a tabular arrangement of the components where columns
have different sizes or one component spans multiple columns. A more complex
layout manager called the grid bag layout can handle these situations. The grid bag
layout is quite complex to use, however, and we do not cover it in this topic; see, for
Search WWH ::




Custom Search