module 2
TRANSCRIPT
![Page 1: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/1.jpg)
Layout Managers
![Page 2: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/2.jpg)
What is a Layout Manager? a object associated with a container
which governs the placement and size of the component
![Page 3: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/3.jpg)
All containers whether top-level or general-purpose
are based on a Layout Manager.
![Page 4: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/4.jpg)
JFrame, JApplet has BorderLayout by default
![Page 5: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/5.jpg)
Why needs Layout Manager?
![Page 6: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/6.jpg)
Normal not Layout Manager: Set an absolute size for components
![Page 7: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/8.jpg)
Okay. Layout Manager takes care of this for you.
![Page 9: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/9.jpg)
LayoutManager based on
minimumSize, preferedSize, maximumSize
to resize and arrange components on the screen
![Page 10: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/10.jpg)
Steps for using Layout Managers 1. setLayout()
Container contentPane = frame.getContentPane(); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
![Page 11: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/12.jpg)
Steps for using Layout Managers
3. pack()
4. setVisible()
![Page 13: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/13.jpg)
Types of Layout Manager
FlowLayout
BorderLayout
![Page 14: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/14.jpg)
Types of Layout Manager
GridLayout
CardLayout
![Page 15: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/15.jpg)
FlowLayout Constructors public FlowLayout()
public FlowLayout(int alignment)
public FlowLayout(int alignment, int hgap, int vgap)
![Page 16: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/16.jpg)
FlowLayout Alignments: CENTER
LEADING
LEFT
RIGHT
TRAILING
![Page 17: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/17.jpg)
FlowLayout Benefits Simple to use
Centers layout for bigger container
Suitable for JApplet
![Page 18: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/18.jpg)
FlowLayout Drawbacks Resize loose alignment
No complex alighment
![Page 19: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/19.jpg)
BorderLayout by default in center
![Page 20: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/21.jpg)
BorderLayout Constructor public BorderLayout()
public BorderLayout(int hgap, int vgap)
![Page 22: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/22.jpg)
BorderLayout Adv. Can specify direction
Dis. Only five locations to add components
No high complexity
![Page 23: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/23.jpg)
GridLayout setLayout(new GridLayout(3, 4))
![Page 24: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/24.jpg)
GridLayout public GridLayout()
public GridLayout(int rows, int columns)
public GridLayout(int rows, int columns, int hgap, int vgap)
![Page 25: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/25.jpg)
GridLayout Adv. Simple to use Useful for rectangular pattern
Same size component
Dis. Cannot create a complex layout
![Page 26: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/26.jpg)
CardLayout
![Page 27: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/27.jpg)
CardLayout CardLayout()
CardLayout(int horizontal_gap, int vertical_gap)
![Page 28: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/28.jpg)
CardLayout
![Page 29: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/29.jpg)
CardLayout void first(Container parent)
void next(Container parent)
void previous(Container parent)
void last(Container parent)
![Page 30: Module 2](https://reader035.vdocuments.mx/reader035/viewer/2022081403/554ebbadb4c905064d8b45b5/html5/thumbnails/30.jpg)
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