Layout managementand event handling

13
Ch 20 Layout Management and Event Handling

description

 

Transcript of Layout managementand event handling

Page 1: Layout managementand event handling

Ch 20

Layout Management and Event Handling

Page 2: Layout managementand 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.

Page 3: Layout managementand event handling

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)

Page 4: Layout managementand event handling

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.

Page 5: Layout managementand event handling

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));

Page 6: Layout managementand event handling

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)

Page 7: Layout managementand event handling

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));

Page 8: Layout managementand event handling

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.

Page 9: Layout managementand event handling

BorderLayout Manager

“Center”

“North”

“South”

“West” “East”

Page 10: Layout managementand event handling

While adding a component a region is specified as an argument to add()

as BorderLayout.NORTH e.g add(new

Button(“OK”),BorderLayout.SOUTH);

Page 11: Layout managementand event handling

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 )

Page 12: Layout managementand event handling

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

Page 13: Layout managementand event handling

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.