0% found this document useful (0 votes)
9 views26 pages

Ch2 - JAVA Layout Managers

The document discusses different Java layout managers including BorderLayout, CardLayout, FlowLayout, GridBagLayout and GridLayout. It explains the constructors and usage of each layout manager with examples and figures. The last section assigns homework to write a program using GridBagLayout.

Uploaded by

moheda214
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)
9 views26 pages

Ch2 - JAVA Layout Managers

The document discusses different Java layout managers including BorderLayout, CardLayout, FlowLayout, GridBagLayout and GridLayout. It explains the constructors and usage of each layout manager with examples and figures. The last section assigns homework to write a program using GridBagLayout.

Uploaded by

moheda214
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
You are on page 1/ 26

Academic Year 2024

Class: BIT28

Chapter 02

JAVA Layout Managers

Faculty of Computing
SIMAD University

JAVA Programming SIMAD University 1


Overview

The Layout Managers are used to arrange components in a


particular manner. The Java Layout Managers facilitates us to
control the positioning and size of the components in GUI
forms.
Layout Manager is an interface that is implemented by all the
classes of layout managers.
One reason that java uses Layout Managers is so that
containers can be resized gracefully.

JAVA Programming SIMAD University 2


The Java.AWT packages provides five Layout Manager
classes.

JAVA Programming SIMAD University 3


FlowLayout and GridLayout are the easiest
Layout manager to understand.

BodredLayout and CardLayout managers are


somewhat harder to use.

GridbagLayout is the most powerful layout


manager but it’s also most complicated.

JAVA Programming SIMAD University 4


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.
There are two 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.
JAVA Programming SIMAD University 5
BorderLayout() constructor

JAVA Programming SIMAD University 6


Figure 2.1 BorderLayout() constructor

JAVA Programming SIMAD University 7


BorderLayout(int hgap, int vgap) constructor

JAVA Programming SIMAD University 8


Figure 2.2 BorderLayout(int hgap, int vgap) constructor

JAVA Programming SIMAD University 9


CardLayout

The Java CardLayout class manages the components in such


a manner that only one component is visible at a time. It treats
each component as a card that is why it is known as
CardLayout.

There are two 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.
JAVA Programming SIMAD University 10
Commonly Used Methods of CardLayout Class

JAVA Programming SIMAD University 11


JAVA Programming SIMAD University 12
JAVA Programming SIMAD University 13
When the button named IT is clicked, we get CS button and then GM

Figure 2.3 CardLayout output

JAVA Programming SIMAD University 14


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.
The FlowLayout layout manager can handle any number of
components.The components are laid out side by side from
left to right.
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.
JAVA Programming SIMAD University 15
JAVA Programming SIMAD University 16
Figure 2.4 FlowLayout output

JAVA Programming SIMAD University 17


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.
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.
JAVA Programming SIMAD University 18
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
 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.
JAVA Programming SIMAD University 19
Example of GridLayout class: Using GridLayout() Constructor

JAVA Programming SIMAD University 20


JAVA Programming SIMAD University 21
Figure 2.5 GridLayout() constructor

The GridLayout() constructor creates only one row. The following


example shows the usage of the parameterless constructor.
JAVA Programming SIMAD University 22
Example of GridLayout class: Using GridLayout(int rows, int columns) Constructor

JAVA Programming SIMAD University 23


JAVA Programming SIMAD University 24
Figure 2.6 GridLayout( int row , int columns ) constructor

The GridLayout((int rows, int columns) ) constructor creates rows


and columns but no gaps between the components.
JAVA Programming SIMAD University 25
Chapter 02 Assignments

Home Work
Individual Home Work
Write one example program about GridBagLayout?

JAVA Programming SIMAD University 26

You might also like