0% found this document useful (0 votes)
113 views5 pages

2-Java Swing Layouts

The document discusses different layout managers in Java Swing - FlowLayout, BorderLayout, and GridLayout. FlowLayout arranges components in a line, BorderLayout divides the container into five regions (north, south, east, west, center) with one component each, and GridLayout arranges components in a grid with equal number of rows and columns. Sample programs demonstrate how to use each layout manager. The objective is to study layout managers and design a calculator application using an appropriate layout.

Uploaded by

Shahzaib Shakir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views5 pages

2-Java Swing Layouts

The document discusses different layout managers in Java Swing - FlowLayout, BorderLayout, and GridLayout. FlowLayout arranges components in a line, BorderLayout divides the container into five regions (north, south, east, west, center) with one component each, and GridLayout arranges components in a grid with equal number of rows and columns. Sample programs demonstrate how to use each layout manager. The objective is to study layout managers and design a calculator application using an appropriate layout.

Uploaded by

Shahzaib Shakir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

JAVA SWING LAYOUTS

OBJECTIVE: To study the layout manager in java Swing.

In Java swing, the Layout manager is used to position all its components, with setting properties,
such as the size, the shape, and the arrangement. Different layout managers could have varies in
different settings on their components.

The following layout managers are the ones that’ll be discussed;


• FlowLayout
• BorderLayout
• GridLayout

➢ FlowLayout

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.

Sample Program#1

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

Output
➢ Border Layout

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.

Sample Program #2

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

➢ Grid Layout

The Java GridLayout class is used to arrange the components in a rectangular grid. One component
is displayed in each rectangle. Below is the sample program using Grid layout parameterized
constructor.
Sample Program #3

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

Output

TASK
Design a calculator using appropriate layout.

You might also like