0% found this document useful (0 votes)
12 views13 pages

Swing 2. Layout Manager in Java Swing

The document provides an overview of various Layout Managers in Java, including BorderLayout, GridLayout, FlowLayout, BoxLayout, and CardLayout, detailing their constructors and usage. Each layout manager is illustrated with example code demonstrating how to arrange components within a JFrame. Additionally, it explains the specific functionalities and characteristics of each layout type.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views13 pages

Swing 2. Layout Manager in Java Swing

The document provides an overview of various Layout Managers in Java, including BorderLayout, GridLayout, FlowLayout, BoxLayout, and CardLayout, detailing their constructors and usage. Each layout manager is illustrated with example code demonstrating how to arrange components within a JFrame. Additionally, it explains the specific functionalities and characteristics of each layout type.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Layout Manager

In Java, Layout Managers is used for arranging the components in order.


LayoutMananger is an interface which implements the classes of the layout
manager.
1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. javax.swing.BoxLayout

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:

1. public static final int NORTH

2. public static final int SOUTH

3. public static final int EAST

4. public static final int WEST

5. public static final int CENTER

import java.awt.*;
import javax.swing.*;
public class BorderDemo1
{
JFrame frame;
BorderDemo1()
{
frame=new JFrame();

JButton box1=new JButton("**NORTH**");;


JButton box2=new JButton("**SOUTH**");;
JButton box3=new JButton("**EAST**");;
JButton box4=new JButton("**WEST**");;
JButton box5=new JButton("**CENTER**");;

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.

There are 3 types of constructor in Grid Layout. They are as following:

1. GridLayout()

2. GridLayout(int rows, int columns)

3. GridLayout(int rows, int columns, int hgap, int vgap)


Example of GridLayout class: Using GridLayout() Constructor
import java.awt.*;
import javax.swing.*;

public class GridLayoutExample


{
JFrame frameObj;

// 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");

// adding buttons to the frame


frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3);
frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6);
frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);

// 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();
}
}

Example of GridLayout class: Using GridLayout(int rows,


int columns) Constructor

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);

// setting grid layout of 3 rows and 3 columns


f.setLayout(new GridLayout(3,3));
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new MyGridLayout();
}
}

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)

Flow Layout using Constructor

// 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");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton b10 = new JButton("10");

// adding the buttons to frame


frameObj.add(b1); frameObj.add(b2); frameObj.add(b3); frameObj.add(
b4);
frameObj.add(b5); frameObj.add(b6); frameObj.add(b7); frameObj.ad
d(b8);
frameObj.add(b9); frameObj.add(b10);

// 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();
}
}

Using FlowLayout(int align) constructor

import java.awt.*;
import javax.swing.*;

public class MyFlowLayout{


JFrame f;
MyFlowLayout(){
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");

// adding buttons to the frame


f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5);

// setting flow layout of right alignment


f.setLayout(new FlowLayout(FlowLayout.LEFT));

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.*;

public class FlowLayoutExample1


{
JFrame frameObj;

FlowLayoutExample1()
{
frameObj = 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");
JButton b10 = new JButton("10");

frameObj.add(b1); frameObj.add(b2); frameObj.add(b3); frameObj.add(


b4);
frameObj.add(b5); frameObj.add(b6); frameObj.add(b7); frame
Obj.add(b8);
frameObj.add(b9); frameObj.add(b10);

frameObj.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 25));

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.*;

public class BoxDemo1 extends Frame {


Button buttonBox[];
public BoxDemo1 ()
{
buttonBox = new Button [5];
for (inti = 0;i<4;i++)
{
buttonBox[i] = new Button ("** Button " + (i + 1)+" **");
add (buttonBox[i]);
}
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
}
public static void main(String args[])
{
BoxDemo1 obj=new BoxDemo1();
}
}

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;

JButton btn1, btn2, btn3;


Container cPane;
CardLayoutExample1()
{

cPane = getContentPane();

crd = new CardLayout();

cPane.setLayout(crd);

btn1 = new JButton("Apple");


btn2 = new JButton("Boy");
btn3 = new JButton("Cat");

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)
{
crd.next(cPane);
}

public static void main(String argvs[])


{

CardLayoutExample1 crdl = new CardLayoutExample1();

crdl.setSize(300, 300);
crdl.setVisible(true);
crdl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

You might also like