0% found this document useful (0 votes)
52 views10 pages

Java Layout Managers Overview

The document provides an overview of various Java LayoutManagers used for arranging GUI components, including BorderLayout, FlowLayout, GridLayout, and GridBagLayout. It details the functionality, constructors, and examples of each layout type, highlighting how they control component positioning and size. Additionally, it mentions the importance of the LayoutManager interface and lists several layout manager classes available in Java.

Uploaded by

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

Java Layout Managers Overview

The document provides an overview of various Java LayoutManagers used for arranging GUI components, including BorderLayout, FlowLayout, GridLayout, and GridBagLayout. It details the functionality, constructors, and examples of each layout type, highlighting how they control component positioning and size. Additionally, it mentions the importance of the LayoutManager interface and lists several layout manager classes available in Java.

Uploaded by

Ranga Timilsina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

unit 4.

Layout Management:
• No Layout,
• Flow layout,
• Border Layout,
• Grid Layout,
• Gridbag Layout,
• Group Layout.
Java LayoutManagers
• The LayoutManagers are used to arrange components in a particular manner.
• The Java LayoutManagers facilitates us 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.
• There are the following classes that represent the layout managers:
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link] etc.
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 a frame
or window.
• The BorderLayout provides five constants for each region:
[Link] static final int NORTH
[Link] static final int SOUTH
[Link] static final int EAST
[Link] static final int WEST
[Link] static final int CENTER

Constructors:
• 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.
Example: BorderLayout
import [Link].*;
[Link](new BorderLayout(10,10));
import [Link].*;
[Link](east,[Link]);
public class BorderLayoutExample {
[Link](west,[Link]);
JFrame f;
[Link](north,[Link]);
JButton north, south, east,
[Link](south,[Link]);
west, center;
[Link](center,[Link]);
BorderLayoutExample(){
f=new JFrame("Border
[Link](JFrame.EXIT_ON_CL
Layout");
OSE);
[Link](400,400);
[Link](true); }
east=new JButton("East"); public static void main(String[] args) {
west=new JButton("West"); new BorderLayoutExample();
south=new JButton("South"); }
north=new JButton("North"); }
center=new
JButton("Center");
Java 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.
Fields of FlowLayout class
[Link] static final int LEFT
[Link] static final int RIGHT
[Link] static final int CENTER
[Link] static final int LEADING
[Link] static final int TRAILING
Constructors of FlowLayout class
[Link](): creates a flow layout with centered alignment and a default 5 unit horizontal
and vertical gap.
[Link](int align): creates a flow layout with the given alignment and a default 5 unit
horizontal and vertical gap.
[Link](int align, int hgap, int vgap): creates a flow layout with the given alignment
Example: FlowLayout
import [Link].*;
import [Link].*;
public class FlowLayoutExample {
JFrame f;
JButton one,two,three,four,five, six,seven;
FlowLayoutExample(){
f=new JFrame("Flow Layout");
one=new JButton("1");
two=new JButton("2");
three=new JButton("3");
four=new JButton("4");
five=new JButton("5");
six=new JButton("6");
[Link](one);[Link](two);[Link](three);
[Link](four);[Link](five);[Link](six);
[Link](new FlowLayout());
[Link](500,500);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new FlowLayoutExample();
}
}
Java GridLayout
• 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
[Link](): creates a grid layout with one column per component in a
row.
[Link](int rows, int columns): creates a grid layout with the given
rows and columns but no gaps between the components.
[Link](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.
Example: GridLayout
import [Link].*;
import [Link].*;
public class FlowLayoutExample {
JFrame f;
JButton one,two,three,four,five, six,seven;
FlowLayoutExample(){
f=new JFrame("Flow Layout");
one=new JButton("1");
two=new JButton("2");
three=new JButton("3");
four=new JButton("4");
five=new JButton("5");
six=new JButton("6");
[Link](one);[Link](two);[Link](three);
[Link](four);[Link](five);[Link](six);
[Link](new GridLayout(3,3,5,5));
[Link](500,500);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new FlowLayoutExample();
}
Java GridBagLayout
• The Java GridBagLayout class is used to align components vertically,
horizontally or along their baseline.
• The components may not be of the same size.
• Each GridBagLayout object maintains a dynamic, rectangular grid of cells.
• Each component occupies one or more cells known as its display area.
• Each component associates an instance of GridBagConstraints. With the
help of the constraints object, we arrange the component's display area on
the grid.
• 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.
• GridBagLayout(): The parameterless constructor is used
to create a grid bag layout manager

You might also like