Layout means arranging the components within the container.
The task of lay outing can be done automatically by the Layout manager.
The layout manager is set by the setLayout( ) method.
If no call to setLayout( ) is made, then the default layout manager is used.
Whenever a container is resized (or sized for the first time), the layout manager is used to
position each of the components within it.
void setLayout(LayoutManager layoutObj)
Layout Manager is an interface that is implemented by all the classes of layoutmanagers. There are following classes that represents the layout managers:
FlowLayout
BorderLayout
GridLayout
CardLayout
GridBagLayout
FlowLayout is the default layout manager.
FlowLayout implements a simple layout style, which is similar to how words flow in a text editor.
Components are laid out from the upper-left corner, left to right and top to bottom.
When no more components fit on a line, the next one appears on the next line.
A small space is left between each component, above and below,as well as left and right.
setLayout(new FlowLayout(FlowLayout.CENTER));
The BorderLayout class implements a common layout style for toplevel windows.
It has four narrow, fixed-width components at the edges and one large area in the center.
The four sides are referred to as
north,
south,
east, and west.
The middle area is called the center.
setLayout(new BorderLayout());
GridLayout lays out components in a two-dimensional grid.
When we instantiate a GridLayout, we define the number of rows and columns.
GridLayout(int numRows, int numColumns)
The CardLayout class is unique among the other layout managers in that it stores several different layouts.
Each layout can be thought of as being on a separate index card in a deck that can be shuffled so that any card is on top at a given time.
This can be useful for user interfaces with optional components that can be dynamically enabled and disabled upon user input.
We can prepare the other layouts and have them hidden, ready to be activated when needed.
Each GridBagLayout object maintains a dynamic rectangular grid of cells, with each component occupying one or more cells, called its display area.
Each component managed by a grid bag layout is associated with an instance of GridBagConstraints that specifies how the component is laid out within its display area.