Layout Managers - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Layout Managers

Description:

... an object that decides how components will be arranged in a container ... When the container changes size, each region grows or shrinks by the same amount ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 10
Provided by: Cher241
Category:

less

Transcript and Presenter's Notes

Title: Layout Managers


1
Layout Managers
  • Layout Manageran object that decides how
    components will be arranged in a container
  • Some types of layout managers
  • BorderLayout
  • FlowLayout
  • GridLayout

2
The 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
3
Example 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)

4
The 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)
5
Example 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)

6
The 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.
7
Example 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)
8
EVENTS, LISTENERS LISTENER METHODS
9
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com