Layout managementand event handling
-
Upload
charli-patel -
Category
Technology
-
view
346 -
download
2
description
Transcript of Layout managementand event handling
Ch 20
Layout Management and Event Handling
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.
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)
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.
Use Layout Managers
Default layout managers Windows (Frames & Dialogs)
BorderLayout Panels & Applets
FlowLayout
setLayout(new FlowLayout());
setLayout(new BorderLayout());
setLayout(new CardLayout(());
setLayout(new GridLayout(rows,columns,xgap,ygap));
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)
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));
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.
BorderLayout Manager
“Center”
“North”
“South”
“West” “East”
While adding a component a region is specified as an argument to add()
as BorderLayout.NORTH e.g add(new
Button(“OK”),BorderLayout.SOUTH);
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 )
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
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.