Layout means the arrangement of components within
the container. The task of layouting the controls is done automatically by
the Layout Manager.
The Java LayoutManagers helps to
control the positioning and size of the components in GUI forms. LayoutManager
is an interface that is implemented by all the classes of layout managers.
The layout manager is associated with every
Container object. Each layout manager is an object of the class that implements
the LayoutManager interface.
There are the following classes that represent the
layout managers:
- java.awt.BorderLayout
- java.awt.FlowLayout
- java.awt.GridLayout
- java.awt.CardLayout
- java.awt.GridBagLayout
1.
Border Layout:
The borderlayout arranges the components to fit in
the five regions: east, west, north, south and center.
Each region (area) may contain one component only.
It is the default layout of a 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.
- BorderLayout(int
hgap, int vgap): creates a border layout with the given
horizontal and vertical gaps between the components.
Ex:
import java.awt.*;
import javax.swing.*;
public class Border
{
JFrame f;
Border()
{
f = new
JFrame();
//
creating buttons
JButton b1
= new JButton("NORTH");; // the button will be labeled as NORTH
JButton b2
= new JButton("SOUTH");; // the button will be labeled as SOUTH
JButton b3
= new JButton("EAST");; // the button will be labeled as EAST
JButton b4
= new JButton("WEST");; // the button will be labeled as WEST
JButton b5
= new JButton("CENTER");; // the button will be labeled as
CENTER
f.add(b1, BorderLayout.NORTH); // b1 will be
placed in the North Direction
f.add(b2,
BorderLayout.SOUTH); // b2 will be
placed in the South Direction
f.add(b3,
BorderLayout.EAST); // b2 will be placed
in the East Direction
f.add(b4,
BorderLayout.WEST); // b2 will be placed
in the West Direction
f.add(b5,
BorderLayout.CENTER); // b2 will be
placed in the Center
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[] args) {
new
Border();
}
}
2.
Flow Layout
It layouts the components in a directional flow. The Java FlowLayout class is used to arrange
the components in a line, one after another (in a flow). It is the default
layout of the 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.
Ex:
// import statements
import java.awt.*;
import javax.swing.*;
public class FlowLayoutExample
{
JFrame frameObj;
// constructor
FlowLayoutExample()
{
//
creating a frame object
frameObj =
new JFrame();
//
creating the buttons
JButton b1
= new JButton("1");
JButton b2
= new JButton("2");
JButton b3
= new JButton("3");
JButton b4
= new JButton("4");
JButton b5
= new JButton("5");
// adding
the buttons to frame
frameObj.add(b1); frameObj.add(b2); frameObj.add(b3); frameObj.add(b4);
frameObj.add(b5);
//
parameter less constructor is used therefore, alignment is center
//
horizontal as well as the vertical gap is 5 units.
frameObj.setLayout(new FlowLayout());
frameObj.setSize(300, 300);
frameObj.setVisible(true);
}
// main method
public static void main(String argvs[])
{
new
FlowLayoutExample();
}
}
3.
Grid Layout
The GridLayout manages the components in form of a
rectangular grid. The Java GridLayout class is used to arrange the
components in a 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 along
with given horizontal and vertical gaps.
Ex:
/ import statements
import java.awt.*;
import javax.swing.*;
public class GridLayoutExample
{
JFrame frameObj;
// constructor
GridLayoutExample()
{
frameObj = new JFrame();
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
// adding buttons to the frame
// since, we are using the parameterless
constructor, therfore;
// the number of columns is equal to the number of
buttons we
// are adding to the frame. The row count remains
one.
frameObj.add(btn1); frameObj.add(btn2);
frameObj.add(btn3);
frameObj.add(btn4); frameObj.add(btn5);
// setting
the grid layout using the parameterless constructor
frameObj.setLayout(new GridLayout());
frameObj.setSize(300, 300);
frameObj.setVisible(true);
}
// main method
public static void main(String argvs[])
{
new GridLayoutExample();
}
}
4.
Card Layout
The CardLayout object treats each component in the
container as a card. Only one card is visible at a time.
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.
Ex:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CardLayoutExample1 extends JFrame
implements ActionListener
{
CardLayout crd;
// button variables to hold the references of
buttons
JButton btn1, btn2, btn3;
Container cPane;
// constructor of the class
CardLayoutExample1()
{
cPane =
getContentPane();
//default
constructor used therefore, components will cover the whole area
crd = new CardLayout();
cPane.setLayout(crd);
// creating the buttons
btn1 = new JButton("Apple");
btn2 = new JButton("Boy");
btn3 = new JButton("Cat");
// adding listeners to it
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
cPane.add("a", btn1); // first card is the
button btn1
cPane.add("b", btn2); // first card is the
button btn2
cPane.add("c", btn3); // first card is the button btn3
}
public void actionPerformed(ActionEvent e)
{
// Upon clicking the button, the next card of the
container is shown
// after the last card, again, the first card of the
container is shown upon clicking
crd.next(cPane);
}
// main method
public static void main(String argvs[])
{
// creating an object of the class
CardLayoutExample1
CardLayoutExample1 crdl = new
CardLayoutExample1();
// size is 300 * 300
crdl.setSize(300, 300);
crdl.setVisible(true);
crdl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
5.
GridBag Layout
This is the most flexible layout manager class.The
object of GridBagLayout aligns the component vertically,horizontally or along
their baseline without requiring the components of same size. Each
GridBagLayout object maintains a dynamic, rectangular grid of cells. Each
component occupies one or more cells known as its display area.
The GridBagLayout manages each component's minimum
and preferred sizes in order to determine the component's size. GridBagLayout
components are also arranged in the rectangular grid but can have many
different sizes and can occupy multiple rows or columns.
Constructor:
GridBagLayout(): The parameterless constructor is used to create a grid bag layout
manager.
Ex:
import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
public class GridBagLayoutExample extends
JFrame{
public
static void main(String[] args) {
GridBagLayoutExample a = new GridBagLayoutExample();
}
public
GridBagLayoutExample() {
GridBagLayoutgrid = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(grid);
setTitle("GridBag Layout Example");
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
gbc.fill =
GridBagConstraints.HORIZONTAL;
gbc.gridx
= 0;
gbc.gridy
= 0;
this.add(new Button("Button One"), gbc);
gbc.gridx
= 1;
gbc.gridy
= 0;
this.add(new Button("Button two"), gbc);
gbc.fill =
GridBagConstraints.HORIZONTAL;
gbc.ipady
= 20;
gbc.gridx
= 0;
gbc.gridy
= 1;
this.add(new Button("Button Three"), gbc);
gbc.gridx
= 1;
gbc.gridy
= 1;
this.add(new Button("Button Four"), gbc);
gbc.gridx
= 0;
gbc.gridy
= 2;
gbc.fill =
GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 2;
this.add(new Button("Button Five"), gbc);
setSize(300, 300);
setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}