0% found this document useful (0 votes)
91 views

Layouts in Unit 5

The document discusses Java layout managers. It explains that a layout manager arranges components within a container and is set using the setLayout() method. The key layout managers covered are FlowLayout, BorderLayout, and GridLayout. FlowLayout positions components left-to-right, top-to-bottom. BorderLayout divides the container into five regions. GridLayout divides the container evenly into rows and columns specified in the constructor. Examples are provided for each layout.

Uploaded by

neha yarrapothu
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Layouts in Unit 5

The document discusses Java layout managers. It explains that a layout manager arranges components within a container and is set using the setLayout() method. The key layout managers covered are FlowLayout, BorderLayout, and GridLayout. FlowLayout positions components left-to-right, top-to-bottom. BorderLayout divides the container into five regions. GridLayout divides the container evenly into rows and columns specified in the constructor. Examples are provided for each layout.

Uploaded by

neha yarrapothu
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

LAYOUTS

• Layout manager arranges your components within a


window.

• Each container object has a layout manager


associated with it.

• A layout manager is an instance of class that


implements the LayoutManager interface.

• The layout manager is set by the setLayout() method.


– Syntax: void setLayout(LayoutManager obj);

Facuty: Mrs. M. Lalitha, Asst. Prof., CSE,


2
GNITS
• Each layout manager keeps track of a list of
components that are stored by their names.

• Each component that is being managed by a


layout manager contains the
getPreferredSize() and getMinimumSize()
methods.

• These methods return the preferred and


minimum size required to display each
component.
Facuty: Mrs. M. Lalitha, Asst. Prof., CSE,
3
GNITS
Layout Manager classes

LayoutManager class Description

FlowLayout Flow layout positions components left to right,


top to bottom.

BorderLayout Border layouts use five components: North,


South, East, West, and Center

GridLayout Grid layout displays components in a two-


dimensional grid.

Facuty: Mrs. M. Lalitha, Asst. Prof., CSE,


4
GNITS
FlowLayout
• The default layout for a Panel/Frame is
FlowLayout.

• Use add(component); to add to a component


when using a FlowLayout.

• Exact layout depends on size of Applet.

• Components are made as small as possible.


Facuty: Mrs. M. Lalitha, Asst. Prof., CSE,
5
GNITS
FlowLayout
• Arranges the components in left to right, top to
bottom fashion.

• Constructors:
public FlowLayout ()
public FlowLayout (int alignment)
public FlowLayout (int alignment, int H_Gap, int
V_Gap)

• Alignments are specified as: FlowLayout.LEFT ;


FlowLayout.RIGHT & FlowLayout.CENTER.

Facuty: Mrs. M. Lalitha, Asst. Prof., CSE,


6
GNITS
example: FlowLayout
import java.awt.*;
import java.applet.*;
public class FlowLayoutExample
extends Applet {
public void init () {
setLayout (new FlowLayout ());
// default
add (new Button ("One"));
add (new Button ("Two"));
add (new Button ("Three"));
add (new Button ("Four"));
add (new Button ("Five"));
add (new Button ("Six"));
}
}

Facuty: Mrs. M. Lalitha, Asst. Prof., CSE,


7
GNITS
BORDER LAYOUT
• It divides the container into 5 regions
( NORTH, SOUTH, EAST, WEST and
CENTER).

• It is the default layout manager for


Window. public BorderLayout().

• To add a component at a specific region,


following method is used:
public void add ( Component c, int
Region) ;

• Example: Button btn = new


Button(“OK”); frm.add( btn,
BorderLayout.EAST);

Facuty: Mrs. M. Lalitha, Asst. Prof., CSE,


8
GNITS
Example: BorderLayout
import java.awt.*;
import java.applet.*;

public class BorderLayoutExample extends Applet {


public void init () {
setLayout (new BorderLayout());
add(new Button("One"), BorderLayout.NORTH);
add(new Button("Two"), BorderLayout.WEST);
add(new Button("Three"),
BorderLayout.CENTER);
add(new Button("Four"), BorderLayout.EAST);
add(new Button("Five"), BorderLayout.SOUTH);
add(new Button("Six"), BorderLayout.SOUTH);
}
}

Facuty: Mrs. M. Lalitha, Asst. Prof., CSE,


9
GNITS
GRID LAYOUT
• The GridLayout manager divides the
container up into a given number of rows and
columns:
new GridLayout(rows, columns)

• All sections of the grid are equally sized and as


large as possible

Facuty: Mrs. M. Lalitha, Asst. Prof., CSE,


10
GNITS
Example: Grid Layout
import java.awt.*;
import java.applet.*;
public class GridLayoutExample
extends Applet {
public void init () {
setLayout(new GridLayout(2,
3));
add(new Button("One"));
add(new Button("Two"));
add(new Button("Three"));
add(new Button("Four"));
add(new Button("Five"));
}
}

Facuty: Mrs. M. Lalitha, Asst. Prof., CSE,


11
GNITS
Other layout managers
Layout manager class Description

CardLayout Card layouts emulate index cards. Only the one on


top is showing.

GridBagLayout Lays different size components within a


flexible grid.

BoxLayout Lays out components vertically or


horizontally with a box.

SpringLayout Lays out components subject to a set of


constraints.

Facuty: Mrs. M. Lalitha, Asst. Prof., CSE,


12
GNITS

You might also like