Layout Managers in Java
Java Layout Managers
The GUI is made up of a number of components; the Layout Managers
affect the content pane.
Layout Managers are provided to arrange GUI components on a
container for presentation purposes. This allows the programmer to concentrate
on the basic "look and feel" and lets the layout managers process
most of the layout details.
There are many layout managers.
Basic Layout Managers
|
||
FlowLayout
|
Default for java.applet.Applet, java.awt.Panel and java.swing.JPanel.
Places components sequentially (left to right) in the order they were added.
You can specify the order.
|
|
BorderLayout
|
Default for the
content panes of JFrame and JApplets. Arranges
the components into five areas: North, South East, West and Center
|
|
GridLayout
|
Arranges the
components into rows and columns
|
|
Advanced Layout Managers
|
||
BoxLayout
|
Allows components to
be arranged left-to-right or top-to-bottom in a container
|
|
CardLayout
|
Stacks components
like a deck of cards
|
|
GridBagLayout
|
similar to GridLayout. unlike GridLayout each
component size can vary and components can be added in any order.
|
|
Java BorderLayout
The
BorderLayout is used to arrange the components in five regions: north, south,
east, west and center. Each region (area) may contain one component only. It is
the default layout of frame or window. The BorderLayout provides five constants
for each region:
- public
static final int NORTH
- public
static final int SOUTH
- public
static final int EAST
- public
static final int WEST
- public
static final int CENTER
Constructors of
BorderLayout class:
- BorderLayout(): creates
a border layout but with no gaps between the components.
- JBorderLayout(int
hgap, int vgap): creates a border layout with the given
horizontal and vertical gaps between the components.
Java GridLayout
The GridLayout is used to arrange the components in rectangular
grid. One component is displayed in each rectangle.
Constructors of
GridLayout class
- GridLayout(): creates a grid layout with one column per
component in a row.
- GridLayout(int
rows, int columns): creates
a grid layout with the given rows and columns but no gaps between the
components.
- GridLayout(int
rows, int columns, int hgap, int vgap): creates
a grid layout with the given rows and columns alongwith given horizontal
and vertical gaps.
Java FlowLayout
The FlowLayout is used to arrange the components in a line, one
after another (in a flow). It is the default layout of applet or panel.
Fields of FlowLayout class
- public
static final int LEFT
- public
static final int RIGHT
- public
static final int CENTER
- public
static final int LEADING
- public
static final int TRAILING
Constructors of FlowLayout class
- FlowLayout(): creates a flow layout with centered alignment and
a default 5 unit horizontal and vertical gap.
- FlowLayout(int
align): creates a flow layout
with the given alignment and a default 5 unit horizontal and vertical gap.
- FlowLayout(int
align, int hgap, int vgap): creates
a flow layout with the given alignment and the given horizontal and vertical
gap.
Java BoxLayout
The
BoxLayout is used to arrange the components either vertically or horizontally.
For this purpose, BoxLayout provides four constants. They are as follows:
Fields of BoxLayout
class
- public
static final int X_AXIS
- public
static final int Y_AXIS
- public
static final int LINE_AXIS
- public
static final int PAGE_AXIS
Constructor of
BoxLayout class
BoxLayout(Container c, int axis): creates
a box layout that arranges the components with the given axis.
Java CardLayout
The CardLayout class manages the components in such a manner that
only one component is visible at a time. It treats each component as a card
that is why it is known as CardLayout.
Constructors of
CardLayout class
- CardLayout(): creates a card layout with zero horizontal and
vertical gap.
- CardLayout(int
hgap, int vgap): creates a card layout
with the given horizontal and vertical gap.
Commonly used methods
of CardLayout class
- public
void next(Container parent): is
used to flip to the next card of the given container.
- public
void previous(Container parent): is
used to flip to the previous card of the given container.
- public
void first(Container parent): is
used to flip to the first card of the given container.
- public
void last(Container parent): is
used to flip to the last card of the given container.
- public
void show(Container parent, String name): is used to flip to the specified card with the
given name.
Comments
Post a Comment