This is the most complicated Layout manager that the awt provides.
It lets you arrange components in rows and columns and specify constraints
on how the components will "grow" to fill the available space.
For example, this
MyFrame.java
file defines a frame with the components shown below.
We can think of the container as a grid using any scale we want. For example
- A is at (0,0) and its size is 10 x 1
- B is at (0,1) and its size is 5 x 8
- C is at (0,9) and its size is 5 x 1
- D is at (5,1) and its size is 5 x 9
When we add each object to the container, we use a GridBag
Constraints object that specifies these dimensions, plus gives a
width (0 to 100) of how much the component should grow in X and Y
relative to other components, and a constant that specifies in
what dimensions it can grow.
You can reuse the GridBag Constraints object for each component as you add it,
since the GridBagLayout manager makes a copy. It's handy to define a method to
set all these constraint values.