Swing 2. Layout Manager in Java Swing
Swing 2. Layout Manager in Java Swing
BorderLayout
BorderLayout is used, when we want to arrange the components in five
regions. The five regions can be north, south, east, west and the centre.
There are 5 types of constructor in Border Layout. They are as following:
import java.awt.*;
import javax.swing.*;
public class BorderDemo1
{
JFrame frame;
BorderDemo1()
{
frame=new JFrame();
frame.add(box1,BorderLayout.NORTH);
frame.add(box2,BorderLayout.SOUTH);
frame.add(box3,BorderLayout.EAST);
frame.add(box4,BorderLayout.WEST);
frame.add(box5,BorderLayout.CENTER);
frame.setSize(400,400);
frame.setVisible(true);
}
public static void main(String[] args)
{
new BorderDemo1();
}
}
Grid Layout
Grid Layout is used, when we want to arrange the components in a
rectangular grid.
1. GridLayout()
// constructor
GridLayoutExample()
{
frameObj = new JFrame();
// creating 9 buttons
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");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
frameObj.setSize(300, 300);
frameObj.setVisible(true);
}
// main method
public static void main(String argvs[])
{
new GridLayoutExample();
}
}
import java.awt.*;
import javax.swing.*;
public class MyGridLayout{
JFrame f;
MyGridLayout(){
f=new JFrame();
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");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
// adding buttons to the frame
f.add(b1); f.add(b2); f.add(b3);
f.add(b4); f.add(b5); f.add(b6);
f.add(b7); f.add(b8); f.add(b9);
Flow Layout
Flow Layout is used, when we want to arrange the components in a
sequence one after another.
There are 3 types of constructor in the Flow Layout. They are as following:
1. FlowLayout()
2. FlowLayout(int align)
3. FlowLayout(int align, inthgap, intvgap)
// import statements
import java.awt.*;
import javax.swing.*;
// constructor
FlowLayoutExample()
{
// creating a frame object
frameObj = new JFrame();
frameObj.setSize(300, 300);
frameObj.setVisible(true);
}
// main method
public static void main(String argvs[])
{
new FlowLayoutExample();
}
}
import java.awt.*;
import javax.swing.*;
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new MyFlowLayout();
}
}
Using FlowLayout(int align, int hgap, int vgap) constructor
// import statement
import java.awt.*;
import javax.swing.*;
FlowLayoutExample1()
{
frameObj = new JFrame();
frameObj.setSize(300, 300);
frameObj.setVisible(true);
}
public static void main(String argvs[])
{
new FlowLayoutExample1();
}
}
Fields of FlowLayout class
1. public static final int LEFT
2. public static final int RIGHT
3. public static final int CENTER
4. public static final int LEADING
5. public static final int TRAILING
Box Layout
Box Layout is used, when we want to arrange the components vertically or
horizontally. BoxLayout (Container c, int axis) is the only constructor in
the Box Layout.
import java.awt.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.*;
public class BoxDemo1 extends Frame {
Button buttonBox[];
public BoxDemo1 ()
{
buttonBox = new Button [2];
for (inti = 0;i<2;i++)
{
buttonBox[i] = new Button ("** Button " + (i + 1)+" **");
add (buttonBox[i]);
}
setLayout (new BoxLayout (this, BoxLayout.X_AXIS));
setSize(500,500);
setVisible(true);
}
public static void main(String args[])
{
BoxDemo1 obj=new BoxDemo1();
}
}
Card Layout
Card Layout is used, when we want to see only one component at a time.
There are 2 types of constructor in the Card Layout. They are as following:
1. CardLayout()
2. CardLayout(inthgap, intvgap)
Important methods
1. first()
2. last()
3. previous()
4. next()
5. show()
// import statements
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CardLayoutExample1 extends JFrame implements ActionLi
stener
{
CardLayout crd;
cPane = getContentPane();
cPane.setLayout(crd);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
crd.next(cPane);
}
crdl.setSize(300, 300);
crdl.setVisible(true);
crdl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}