Universidad Nacional de Colombia - PowerPoint PPT Presentation

1 / 79
About This Presentation
Title:

Universidad Nacional de Colombia

Description:

scroll bars, scroll panes, text areas, text fields, and. other panels. Panels ... how big the component would like to be, barring conflict with a layout manager ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 80
Provided by: proyectoun
Category:

less

Transcript and Presenter's Notes

Title: Universidad Nacional de Colombia


1
C
ertificación en
J
AVA
  • Universidad Nacional de Colombia
  • Facultad de Ingeniería
  • Departamento de Sistemas

2
9. LAYOUT MANAGERS
  • Objectives
  • Why Java uses Layout Managers
  • Layout Manager Theory
  • Layout Policies

3
Objectives
Write code using component, container, and layout
manager classes of the java.awt package to
present a GUI with specified appearance and
resize behavior, and distinguish the
responsibilities of layout managers from those
of containers
4
Introduction
The Abstract Windowing Toolkit (AWT) provides a
handful of layout managers, each of which
implements its own layout policy In Java, you
create a GUI by choosing one or more layout
managers and letting them take care of the
details.
5
  • When you started working with layout managers,
    you probably had two impressions
  • You no longer bore the burden of specifying the
    exact position and dimensions of each component
  • You no longer had the power to specify the exact
    position and dimensions of each component

6
  • Some people enjoy working with layout managers,
    and others resent them
  • They are here to stay, so the job at hand is to
    master this feature of the language. Acquiring
    this competence requires three things
  • An understanding of why Java uses layout managers
  • An understanding of the layout policies of the
    more basic layout managers
  • Some practice

7
Why Java Uses Layout Managers
The theory lies in the position that precise
layout (that is, specification in pixels of each
component's size and position) is a repetitious
and often-performed task therefore, according to
the principles of object-oriented programming,
layout functionality ought to be encapsulated
into one or more classes to automate the task

8
The practical reason for having layout managers
systems from Java's platform independence. Java
components borrow their behavior from the window
system of the underlying hardware on which the
Java Virtual Machine is running. Thus on a
Macintosh, an AWT button looks like any other Mac
button on a Motif platform, a Java button looks
like any other Motif button, and so on
9
 
Problem buttons and other components have
different sizes when instantiated on different
platforms!!
10
On a Windows 95 machine 32 pixels wide by 21
pixels high. On a Motif platform 32 pixels
wide, but 22 pixels high, even though it uses the
same font The difference seems small until you
consider the effect such a difference would have
on a column of many buttons.
11
Layout Manager Theory
There are five layout manager classes in the AWT
toolkit java.awt.LayoutManager it is an
interface, not a class, because the layout
managers are so different from one another that
they have nothing in common except a handful of
method names java.awt.LayoutManager2
interface the GridBag, Border, and Card layout
managers implement it Layout managers work in
partnership with containers
12
Containers and Components
Containers are Java components that can contain
other components There is a java.awt.Container
class which, like java.awt.Button and
java.awt.Choice, inherits from the
java.awt.Component superclass
13
Inheritance of java.awt.Container
14
The Container class was abstract, but now it
isn't its most commonly used concrete subclasses
are Applet, Frame, and Panel, (Note that Applet
is a subclass of Panel)
15
Container
Panel
Window
ScrollPane
Applet
Frame
16
If Java encouraged precise pixel-level sizing
and positioning, there would be a lot of Java
GUIs that looked exquisite on their platform of
origin, and terrible on other hosts There is no
guarantee that fonts with identical names will
truly be 100 percent identical from platform to
platform there could be minute differences.
Therefore, Java cannot even guarantee that two
strings drawn with the same text and font will
display at the same size across platforms
17
Java GUIs reside in applets or in frames Simple
applets Put your components in your
applet Simple applications put your components
in your frame. In both cases, you might wonder
how the components end up where they do layout
managers are lurking in the background, taking
care of details)
18
More complicated GUIs it is convenient to
divide the applet or frame into smaller regions
These regions might constitute, for example, a
toolbar or a matrix of radio buttons GUI
sub-regions are implemented most commonly with
the Panel container.
19
Panels
  • can contain other components
  • buttons,
  • canvases,
  • check boxes,
  • scroll bars,
  • scroll panes,
  • text areas,
  • text fields, and
  • other panels

20
  • Complicated GUIs sometimes have very complicated
    containment hierarchies of panels within panels
    within panels within panels, and so on, down
    through many layers of containment
  • In Java, the term hierarchy is ambiguous.
  • When discussing classes, hierarchy refers to the
    hierarchy of inheritance from superclass to
    subclass
  • When discussing GUIs, hierarchy refers to the
    containment hierarchy of applets or frames, which
    contain panels containing panels containing panels

21
Containment Hierarchy Example
Red
Green
Blue
145
45
195
22
(No Transcript)
23
(No Transcript)
24
Containment hierarchy
25
  • A component inside a container receives certain
    properties from the container
  • Font, foreground and background color same as
    the container
  • Panels default layout manager Flow
  • Applets default layout manager Flow
  • Frames default layout manager Border

26
  • Each panel is built in four steps
  • Construct the panel
  • Give the panel a layout manager
  • Populate the panel
  • Add the panel to its own container

27
Component Size and position
Components know where they are and how big they
are The java.awt.Component class has four
instance variables x,y position of the
components upper left corner width, and height
in pixels
28
Position and Size
29
A components size and position can be changed by
calling the components setBounds() method
C
setBounds(x, y, width, height)
30
Line 7 has no effect, because after it executes,
the button is added to the applet. The applet
calls on its layout manager to enforce its layout
policy on the button
31
A disappointing Button
32
Layout Policies
Every Java component has a preferred size The
preferred size expresses how big the component
would like to be, barring conflict with a layout
manager Preferred size is generally the smallest
size necessary to render the component in a
visually meaningful way
33
a buttons preferred size is the size of its
label text, plus a little border of empty space
around the text, plus the shadowed decorations
that mark the boundary of the button Thus a
button's preferred size is "just big
enough. Preferred size is platform dependent,
since component boundary decorations vary from
system to system
34
The Flow Layout Manager
arranges components in horizontal rows Default
layout manager type for panels and applets
35
(No Transcript)
36
Component Size and position
37
A narrower applet
Applet ViewerNeatR...
Beowulf
Name
Ok
Applet started.
38
An even narrower applet
39
The Flow Layout Manager
40
A right-justifying Flow layout manager
Applet Viewer FlowRight.class
Applet started.
41
A narrower right-justifying Flow layout manager
42
EJEMPLO
43
(No Transcript)
44
The Grid Layout Manager
always ignores a component's preferred size The
Grid layout manager subdivides its territory into
a matrix of rows and columns The number of rows
and number of columns are specified as parameters
to the manager's constructor
GridLayout(int nRows, int ncolumns)
45
Components know where they are and how big they
are
46
(No Transcript)
47
(No Transcript)
48
EJEMPLO
Components know where they are and how big they
are
49
(No Transcript)
50
The Border Layout Manager
The Flow layout manager always honors a
component's preferred size the Grid layout
manager never does. The Border layout manager
does something in between It is the default
manager for frames
51
The Border layout manager divides its territory
into five regions North South East West Center Ea
ch region may contain a single component No
region is required to contain a component!!
52
(No Transcript)
53
The Border Layout Manager
Components know where they are and how big they
are
54
  • The overloaded version of add() takes two
    parameters
  • The component being added
  • An Object
  • Proper use of the Border Layout manager requires
    that the second parameter be a String that
    specifies the name of the region
  • North
  • South
  • East
  • West
  • Center

55
  • Border Layout constants to use instead of the
    strings
  • BorderLayout.NORTH
  • BorderLayout.SOUTH
  • BorderLayout.EAST
  • BorderLayout .WEST
  • BorderLayout.CENTER

56
East and West
57
East and West, with North
58
East and West, with South
59
East and West, with both North and South
60
(No Transcript)
61
Center
62
(No Transcript)
63
Center, no North
64
Center, no South, East or West
65
(No Transcript)
66
(No Transcript)
67
Other Layout Options
If you are in a situation where Flow, Grid, and
Border will not create the layout you need, your
choices are   To use a GridBag layout
manager To use a Card layout manager To use
no layout manager To create your own layout
manager
68
GridBag Layout Manager
is by far the most complicated layout manager It
divides its container into an array of cells, but
(unlike the cells of a Grid layout manager)
different cell rows can have different heights,
and different cell columns can have different
widths A component can occupy a single cell or
it can span a number of cells. A GridBag layout
manager requires a lot of information to know
where to put a component
69
GridBag Layout Manager ...
A helper class called GridBagConstraints is used
to hold all the layout position information.
When you add a component, you use the
add(Component, Object) version of the add()
method, passing an instance of GridBagConstraints
as the Object parameter.
70
(No Transcript)
71
(No Transcript)
72
(No Transcript)
73
(No Transcript)
74
(No Transcript)
75
(No Transcript)
76
Card Layout Manager ...
Lays out its components in time rather than in
space At any time, a component using a Card
layout manager is displaying one or another of
its components all the other components are
unseen
77
(No Transcript)
78
You always have the option of using no layout
manager at all
myContainer.setLayout(null)
79
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com