2. Layout Managers
Removes repetitious need to lay out
components precisely.
Insulates developer from the fact that
components have different sizes when
instantiated on different platforms.
All implement java.awt.LayoutManager
interface.
3. Layout of Components
FlowLayout
left to right & top
down
BorderLayout
north, south,
west, east &
center
CardLayout
stack of panels
GridLayout
tabular form
(rows &
columns)
GridBagLayout
tabular
form(variable
row heights and
column widths)
4. Component Size and Position
Sizes and positions are merely suggestions,
overridden by the LayoutManager.
Each Component has a preferred size, which
is “just big enough” to display the component.
6. FlowLayout
The flow layout is the default layout manager for all
Panel objects and applets.
It simply lays out components in a single row, from
left to right starting a new row if its container is not
sufficiently wide .
Default alignment is CENTER.
new FlowLayout() // default is centered with 5 pixel
gaps
new FlowLayout(int align)
new FlowLayout(int align, int hgap, int vgap)
7. GridLayout
GridLayout lays out components in a rectangular grid,
where all cells are equal size.
Components are addes cell by cell left to right ,filling
top row and then moving down
GridLayout()
default of one column per component, in a single row.
GridLayout(int rows, int cols)
Creates a grid layout with the specified number
of rows and columns.
GridLayout(int rows, int cols, int hgap, int vgap)
Creates a grid layout with the specified number
of rows and columns.
e.g setLayout(new GridLayout(3,5,10,15));
8. BorderLayout
Divides the container into five regions: east,
south, west, north, and center.
Takes maximum 5 components only, one per
region.
Resizes each component to match the size of
its region.
Resizes each region when the container is
resized.
10. While adding a component a region is
specified as an argument to add()
as BorderLayout.NORTH
e.g add(new
Button(“OK”),BorderLayout.SOUTH);
11. GridBagLayout
GridBagLayout is a flexible layout manager
that aligns components vertically and
horizontally, without requiring that the
components be the same size .
GridBagLayout (int gridx , int gridy, int
gridwidth ,int gridheight ,int weightx,
double weighty double anchor, int fill ,int
insets , int ipadx int ipady )
12. gridx,gridy : define col ,and row position of upper left
corner.
gridwidth ,gridheight : no of cells occupied by
components .default is 1 for both
weightx,weighty: Specifies how to distribute extra
horizontal space
anchor:It determines where, within the display area,
to place the component.
fill:This field is used when the component's display
area is larger than the component's requested size.
Insets:This field specifies the external padding of the
component, the minimum amount of space between
the component and the edges of its display area.
Ipadx, ipady:This field specifies the internal padding
13. CardLayout:The CardLayout class lets you
implement an area that contains different
components at different times .
CardLayout()
Creates a new card layout with gaps of
size zero .
CardLayout(int hgap, int vgap)
Creates a new card layout with the
specified horizontal and vertical gaps.