Title: Layout Managers
1Layout Managers
- Layout Manageran object that decides how
components will be arranged in a container - Some types of layout managers
- BorderLayout
- FlowLayout
- GridLayout
2The Border Layout Manager
- Five regions that can each have one component
added to them
contentPane.setLayout(new BorderLayout()) . .
. contentPane.add(label1, BorderLayout.NORTH)
The CENTER region grows the most when the
container grows and shrinks the most when the
container shrinks
3Example of BorderLayout
- setTitle("Layout Demonstration")
- Container contentPane getContentPane()
- contentPane.setLayout(new BorderLayout())
- JLabel label1 new JLabel("First label here")
- contentPane.add(label1, BorderLayout.NORTH)
- JLabel label2 new JLabel("Second label here")
- contentPane.add(label2, BorderLayout.SOUTH)
- JLabel label3 new JLabel("Third label here")
- contentPane.add(label3, BorderLayout.CENTER)
- JLabel label4 new JLabel("4TH label here")
- contentPane.add(label4, BorderLayout.WEST)
4The Flow Layout Manager
- The simplest layout manager
- Displays components from left to right in the
order they are added to the container - add method has one parameter which is the
component to add
Container contentPane getContentPane() contentP
ane.setLayout(new FlowLayout()) JLabel label1
new JLabel(First label here) contentPane.add(la
bel1) JLabel label2 new JLabel(Second label
there) contentPane.add(label2)
5Example of FlowLayout
- setTitle("Layout Demonstration")
- Container contentPane getContentPane()
- contentPane.setLayout(new FlowLayout())
- JLabel label1 new JLabel("First label here")
- contentPane.add(label1)
- JLabel label2 new JLabel("Second label here")
- contentPane.add(label2)
- JLabel label3 new JLabel("Third label here")
- contentPane.add(label3)
- JLabel label4 new JLabel("4TH label here")
- contentPane.add(label4)
6The Grid Layout Manager
- Specify a number of rows and columns
- All regions in the grid are equal size
- When the container changes size, each region
grows or shrinks by the same amount
contentPane.setLayout(new GridLayout(2, 3)) . .
. contentPane.add(label1) contentPane.add(label2)
Creates a grid layout with two rows and three
columns.
Rows are filled before columns.
7Example of GridLayout
contentPane.setLayout(new GridLayout(2,2)) JLabe
l label1 new JLabel("First label
here") contentPane.add(label1) JLabel label2
new JLabel("Second label here") contentPane.add(l
abel2) JLabel label3 new JLabel("Third label
here") contentPane.add(label3) JLabel label4
new JLabel("4TH label here") contentPane.add(labe
l4)
8EVENTS, LISTENERS LISTENER METHODS
9(No Transcript)