module 2

30
Layout Managers

Upload: tuan-ngo

Post on 10-May-2015

245 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Module 2

Layout Managers

Page 2: Module 2

What is a Layout Manager? a object associated with a container

which governs the placement and size of the component

Page 3: Module 2

All containers whether top-level or general-purpose

are based on a Layout Manager.

Page 4: Module 2

JFrame, JApplet has BorderLayout by default

Page 5: Module 2

Why needs Layout Manager?

Page 6: Module 2

Normal not Layout Manager: Set an absolute size for components

Page 7: Module 2

What happens when the user resize a window? How does my component look like?

How you guys solve platform-dependent positioning?

Page 8: Module 2

Okay. Layout Manager takes care of this for you.

Page 9: Module 2

LayoutManager based on

minimumSize, preferedSize, maximumSize

to resize and arrange components on the screen

Page 10: Module 2

Steps for using Layout Managers 1. setLayout()

Container contentPane = frame.getContentPane(); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));

Page 11: Module 2

Steps for using Layout Managers 2. specify size | void setPreferredSize(Dimension preferredSize)

| void setMinimumSize(Dimension minimumSize)

| void setMaximumSize(Dimension maximumSize)

| btnOk.setPreferredSize(new Dimension (20,20))

Page 12: Module 2

Steps for using Layout Managers

3. pack()

4. setVisible()

Page 13: Module 2

Types of Layout Manager

FlowLayout

BorderLayout

Page 14: Module 2

Types of Layout Manager

GridLayout

CardLayout

Page 15: Module 2

FlowLayout Constructors public FlowLayout()

public FlowLayout(int alignment)

public FlowLayout(int alignment, int hgap, int vgap)

Page 16: Module 2

FlowLayout Alignments: CENTER

LEADING

LEFT

RIGHT

TRAILING

Page 17: Module 2

FlowLayout Benefits Simple to use

Centers layout for bigger container

Suitable for JApplet

Page 18: Module 2

FlowLayout Drawbacks Resize loose alignment

No complex alighment

Page 19: Module 2

BorderLayout by default in center

Page 20: Module 2

BorderLayout Alignment

AFTER_LAST_LINE, PAGE_END, SOUTH

AFTER_LINE_ENDS, LINE_END, EAST

BEFORE_FIRST_LINE, PAGE_START, NORTH

BEFORE_LINE_BEGINS, LINE_START, WEST

CENTER

Page 21: Module 2

BorderLayout Constructor public BorderLayout()

public BorderLayout(int hgap, int vgap)

Page 22: Module 2

BorderLayout Adv. Can specify direction

Dis. Only five locations to add components

No high complexity

Page 23: Module 2

GridLayout setLayout(new GridLayout(3, 4))

Page 24: Module 2

GridLayout public GridLayout()

public GridLayout(int rows, int columns)

public GridLayout(int rows, int columns, int hgap, int vgap)

Page 25: Module 2

GridLayout Adv. Simple to use Useful for rectangular pattern

Same size component

Dis. Cannot create a complex layout

Page 26: Module 2

CardLayout

Page 27: Module 2

CardLayout CardLayout()

CardLayout(int horizontal_gap, int vertical_gap)

Page 28: Module 2

CardLayout

Page 29: Module 2

CardLayout void first(Container parent)

void next(Container parent)

void previous(Container parent)

void last(Container parent)

Page 30: Module 2

CardLayout Adv. Containers and their associated components to share the same space in the container

Dis. Not visually appealing as a Tabbed pane

Requires other components like buttons or drop-down to flip through