Layout Managers - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Layout Managers

Description:

The Container myPC is now a 'Content environment', an 'Event environment', and a ' ... When you add components to a container, the arguments you specify to the add ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 13
Provided by: msresearch
Category:

less

Transcript and Presenter's Notes

Title: Layout Managers


1
Layout Managers
2
A Container manages all PC resources
  • Its brilliant, really

Content current PC state
Layout presentation
Events operator actions
Container
3
Creating a ContentPane container
  • // a container to hold your computers info
  • Container myPC new Container()
  • // current state
  • myPC getContentPane( )
  • The Container myPC is now a Content
    environment, an Event environment, and a
    Layout environment
  • which is an object that holds the current details
    of all screen, user, keyboard, mouse events and
    conditions, and further... is a place where we
    can build a Graphical User Interface (GUI),
    compatible with the computers configuration.

4
what does this do?
  • Container myPC new Container()
  • myPC getContentPane( )
  • FlowLayout layout new FlowLayout( )
  • myPC.setLayout( layout )

JFrame
Container
5
more JFrame and Container methods
  • setSize( w, h )
  • setLocation( x, y )
  • setDefaultCloseOperation( EXIT_ON_CLOSE )
  • Container myPC new Container()
  • myPC getContentPane()
  • FlowLayout layout new FlowLayout()
  • myPC.setLayout( layout )
  • JButton helloButton new JButton( "Hello" )
  • myPC.add( helloButton ) // note added to
    the Container
  • setVisible( true )

6
the Container controls everything
We now shift from using JFrame as the Graphics
organizer, to using the Container
7
Using Layout Managers
  • A layout manager is an object that determines the
    size and position of the components within a
    container. Although components can provide size
    and alignment hints, a container's layout manager
    has the final say on the size and position of the
    components within the container.

8
From Sun
  • Although we recommend that you use layout
    managers, you can perform layout without them. By
    setting a container's layout property to null -
    myPC.setLayout( null ) - you make the container
    use no layout manager. With this strategy, called
    absolute positioning, you must specify the size
    and position of every component within that
    container. One drawback of absolute positioning
    is that it doesn't adjust well when the top-level
    container is resized. It also doesn't adjust well
    to differences between users and systems, such as
    different font sizes.

9
Adding components
  • When you add components to a container, the
    arguments you specify to the add method depend on
    the layout manager that the container is using.
    For example, BorderLayout requires that you
    specify the area to which the component should be
    added, using code like this
  • myPC.add( aComponent, BorderLayout.NORTH)

10
Flow Layout
  • Container myPC new Container()
  • myPC getContentPane()
  • FlowLayout layout new FlowLayout()
  • myPC.setLayout( layout )
  • JButton helloButton new JButton( "Hello" )
  • myPC.add( helloButton )

11
Grid Layout
  • Container myPC new Container()
  • myPC getContentPane()
  • GridLayout layout new GridLayout()
  • myPC.setLayout( layout )
  • JButton helloButton new JButton( "Hello" )
  • myPC.add( helloButton )

12
Border Layout
  • Container myPC new Container()
  • myPC getContentPane()
  • BorderLayout layout new BorderLayout()
  • myPC.setLayout( layout )
  • JButton helloButton new JButton( "Hello" )
  • myPC.add( helloButton, BorderLayout.NORTH )
Write a Comment
User Comments (0)
About PowerShow.com