540 likes | 747 Views
A First Look at GUI Applications. Part III: Layout Managers. Contents. What is Layout Manager? FlowLayout Manager BorderLayout Manager GridLayout Manager. A. What is Layout Manager?. A layout manager is an object that governs the positions and sizes of components in a container
E N D
A First Look at GUI Applications Part III: Layout Managers
Contents • What is Layout Manager? • FlowLayout Manager • BorderLayout Manager • GridLayout Manager
A. What is Layout Manager? • A layout manager is an object that governs the positions and sizes of components in a container • Swing provides a variety of layouts: FlowLayout, BorderLayout, GridLayout, Null Layout… • Use method containers’ setLayout to change its layout container.setLayout(new BorderLayout()) • Default layout for JFrame is BorderLayout and FlowLayout for JPanel
B. FlowLayout Manager • What is FlowLayout? • Flow Window Problem
I. What is FlowLayout? • Components appear horizontally, from left to right, in the order that they were added. When there is no more room in a row, the next components will be dropped to the next row • FlowLayout’s constructors • FlowLayout() • FlowLayout(int align): the align can be FlowLayout.CENTER, FlowLayout.LEFT, or FlowLayout.RIGHT • FlowLayout(int align, int hgap, int vgap) where hgap and vgap are a gap of pixels among components, horizontally and vertically
II. Flow Window Problem • Develop a program that lays three buttons on the horizontal of window has the size 400x70
Solution Project name: FlowWindow • Analysis and Design • Developing the View
2. Developing the View Package name: flowwindow 2.1. Designing the View 2.2. Developing the FlowWindowViewTest Class 2.3. Developing the FlowWindowView Class
2.1. Designing the View • Title: Flow Layout • Size: 400x70 • Layout: Flow Layout • Button 1 • Button 2 • Button 3
2.3. Developing the FlowWindowView Class • Set the layout • Add the buttons
B. BorderLayout Manager • What is BorderLayout? • Border Window Problem • Border Panel Window Problem
I. What is BorderLayout? • BorderLayout manages five regions where components can be placed
I. What is BorderLayout? (cont.) • BorderLayout’s constructors: • BorderLayout • BorderLayout(int hgap, int vgap) • Components are added to a border layout container: container.add(Component comp, String region) Where the region is one of the following values: • BorderLayout.NORTH • BorderLayout.SOUTH • BorderLayout.EAST • BorderLayout.WEST • BorderLayout.CENTER
II. Border Window Problem • Develop a program that has interface as below:
Solution Project name: BorderWindow • Analysis and Design • Developing the View
2. Developing the View Package name: borderwindow 2.1. Designing the View 2.2. Developing the BorderWindowViewTest Class 2.3. Developing the BorderWindowView Class
2.1. Designing the View • Title: Border Layout • Size: 400x300 • Layout: Border Layout • North button • West button • South button • East button • Center button
2.3. Developing the BorderWindowView Class • Don’t need to set the layout here because border layout is used for a frame by default • Add the buttons
III. Border Panel Window Problem • Enhance the border window problem in order to reach the following interface:
Solution Project name: BorderPanelWindow • Analysis and Design • Developing the View
2. Developing the View Package name: borderpanelwindow 2.1. Designing the View 2.2. Developing the BorderPanelWindowViewTest Class 2.3. Developing the BorderPanelWindowView Class
2.1. Designing the View • Title: Border Layout • Layout: Border Layout • North button panel • North button • West button panel • West button • South button panel • South button • East button panel • East button • Center button panel • Center button
2.3. Developing the BorderPanelWindowView Class 2.3.1. Developing the North Button Panel 2.3.2. Developing the West Button Panel 2.3.3. Developing the Other Button Panels
D. GridLayout Manager • What is GridLayout? • Grid Window Example • Grid Panel Window Example
I. What is GridLayout? • GridLayout creates a grid with rows and columns, much like a spreadsheet. A container that is managed by a GridLayout object is divided into equally sized cells • Each cell can hold only one component • A component that is placed in a cell is automatically resized to fill up any extra space columns rows
I. What is GridLayout? (cont.) • GridLayout’s constructors: • GridLayout() • GridLayout(int rows, int cols) • GridLayout(int rows, int cols, int hgap, int vgap) • If rows or cols is declared as zero, it means no limit for rows or columns, but not for both
II. Grid Window Example • Develop a program that has six buttons laid in a grid of 2x3
Solution Project name: GridWindow • Analysis and Design • Developing the View
2. Developing the View Package name: gridwindow 2.1. Designing the View 2.2. Developing the GridWindowViewTest Class 2.3. Developing the GridWindowView Class
2.1. Designing the View • Title: Grid Layout • Size: 400x200 • Layout: Grid Layout (2x3) • Six Buttons
2.3. Developing the GridWindowView Class • Set the layout • Add the buttons
III. Grid Panel Window Example • Develop a program that has six labeled buttons laid in a grid as below:
Solution Project name: GridPanelWindow • Analysis and Design • Developing the View
2. Developing the View Package name: gridpanelwindow 2.1. Designing the View 2.2. Developing the GridPanelWindowViewTest Class 2.3. Developing the GridPanelWindowView Class
2.1. Designing the View • Title: Grid Layout • Size: 400x200 • Layout: Grid Layout (2x3) • Button Panel 1 • Label: This is cell 1. • Button 1 . . . • Button Panel 6 • Label: This is cell 6. • Button 6