Overview of Layout Managers
| Layout Manager | Description |
|---|---|
| BoxLayout | Arranges components in a linear sequence. |
| BorderLayout | Assigns components to one of 5 regions based on cardinal directions. |
| BoxLayout | Arranges items in a linear sequence (either horizontally or vertically). |
| FlowLayout | Adds components from left to right in the order in which they where added. |
| GridLayout | Arranges components in 2D grid of equal sized cells. |
- There are more, but these are the ones covered in CIS2430.
BorderLayout
- Assigns components to one of 5 regions based on cardinal directions.
BorderLayout.NORTHBorderLayout.SOUTHBorderLayout.EASTBorderLayout.WESTBorderLayout.CENTER
- Added to a
JFrameusing itssetLayoutmethod.- ie.
setLayout(new BorderLayout).
- ie.
- Components are added to a region using
JFrame’saddmethod.- ie.
add(label, BorderLayout.CENTER). - Two parameters, first for label (String), second for region.
- ie.
BoxLayout
- Arranges items in a linear sequence (either horizontally or vertically).
- Components are placed in the order they are added.
- Optimized to fill the whole space with the existing components that are visible.
- Components are added to a region using
JFrame’saddmethod.- ie.
add(label). - One parameter for label (String).
- ie.
FlowLayout
- Adds components from left to right in the order in which they where added.
- The default layout manager which is used when none other is set.
- Components are added to a region using
JFrame’saddmethod.- One parameter for label (String).
GridLayout
- Arranges components in 2D grid of equal sized cells.
- Added to a
JFrameusing itssetLayoutmethod.- ie.
setLayout(new GridLayout(rows, cols)).
- ie.
- Components are added to a region using
JFrame’saddmethod.- One parameter for label (String).
- Each component is stretched so that it completely fills a cell.