CS4812 Java for Jocks - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

CS4812 Java for Jocks

Description:

CS4812 -- Java for Jocks. Lecture 5: Components, Containers and Layout Managers ... This week, we cover some basics on components, containers and layout--the ... – PowerPoint PPT presentation

Number of Views:74
Avg rating:3.0/5.0
Slides: 32
Provided by: davidd1
Category:
Tags: cs4812 | java | jocks

less

Transcript and Presenter's Notes

Title: CS4812 Java for Jocks


1
CS4812 -- Java for Jocks
  • Lecture 5 Components, Containers and Layout
    Managers

2
Review JDK 1.1 Events
  • Last week, we say how the JDK 1.1 event model
    gives you three basic models to choose from
  • Listener Interfaces
  • Adapter Classes
  • Semantic Events
  • This week, we cover some basics on components,
    containers and layout--the business end of your
    event-driven GUI.

3
Layout Managers
  • Defined LayoutManager is an interface specifying
    the following
  • void addLayoutComponnet (String n, Component c)
  • void removeLayoutComponent (String n, Comonent
    c)
  • Dimension preferredLayoutSize (Container parent)
  • Dimension minimumLayoutSize (Container parent)
  • void layoutContainer (Container parent)

4
LayoutManager position and size
  • Layout managers are used to calculate preferred
    and minimum sized for a container
  • They also arrange component within the container
  • Each container has associated with it exactly one
    layout manager
  • But a single layout manager may do double duty,
    performing the layout for multiple containers

5
A simple example . . .
  • Public class MyPanel extends Container
  • static LayoutManager fLayout new FlowLayout()
  • . . .
  • Public MyPanel()
  • setLayout(fLayout())
  • . . .

6
LayoutManagers Two General Flavors
  • One can conceptually divide layout managers into
    two types
  • those that attach constraints to their
    components, and . . .
  • . . . those that do not
  • What does this mean? If a manager attaches
    contraints to a component, information about a
    components location (e.g., compass points) is
    generated with the object.

7
LayoutManagers Constraints
  • BorderLayout specifies compass regions of a
    container

8
LayoutManagers Constraints
  • BorderLayouts then append constraint information
    on all components
  • E.g.
  • this.setLayout (new BorderLayout())
  • Button e new Button (East)
  • Button w new Button (West)
  • add(e, East)
  • add(w, West)

9
LayoutManagers Constraints
10
LayoutManagers Another Example
  • import java.awt.
  • import java.applet.
  • public class test extends Applet
  • String Compass "North", "South", "East",
    "West", "Center"
  • public void init()
  • / ALWAYS call super init! /
  • super.init()
  • / set layout /
  • setLayout(new BorderLayout())
  • for (int iCompass.length i--gt0)
  • add (new Button (Compassi),
    Compassi)

11
LayoutManager Example
  • Thus

12
LayoutManager No Constraints
  • The second type of LayoutManager does not specify
    constraints for the objects it holds.
  • Examples
  • GridLayout()
  • FlowLayout()
  • Without constraints, you cannot accurately
    predict layout behavior across platforms

13
LayoutManager No Constraints
  • Example FlowLayout
  • import java.awt.
  • import java.applet.
  • public class test extends Applet
  • public void init()
  • super.init()
  • String Labels "Short", "Short", "Long
    Label", "Really Long Label", "Really, really
    long"
  • setLayout(new FlowLayout())
  • for (int iLabels.length i--gt0)
  • add (new Button (Labelsi))

14
LayoutManager No Constraints
  • Thus

15
LayoutManager No Constraints
  • And also

16
LayoutManager No Constraints
  • Note
  • Since pixels, fonts and insets vary with each
    platform, layout without constraints will vary
    greatly.
  • Lesson
  • Use layout managers without constraints only when
    you have one or few components

17
LayoutManager No Constraints
  • Do not suppose that layout manager without
    constraints are not useful!
  • One of the most useful constraint-free layout
    manager is GridLayout.
  • public GridLayout()
  • public GridLayout(int rows, int cols)
  • public GridLayout(int rows, int cols, int hgap,
    int vgap)

18
GridLayout Example
  • import java.awt.
  • import java.applet.
  • public class test extends Applet
  • public void init()
  • super.init()
  • setLayout(new GridLayout(4,3,5,5))
  • int off-2,2,0
  • for (int i10 i--gt1)
  • add (new Button (""(ioffi3)))
  • add (new Button ("."))
  • add (new Button ("0"))
  • add (new Button ("/-"))
  • add (new MyPanel(null))

19
GridLayout Example
  • Thus

20
Insets and LayoutManagers
  • Layout management can be complemented by the use
    of Insets
  • All containers have Insets--margins within which
    you may paint, but not position components
  • unless a null layout is set!

21
Insets--Did you know?
  • When components are arranged in a container, Java
    calls the containers getInsets() accessor to
    determine margins.
  • Insets vary with both the peer and the
    component--theres no way to know the default
    inset value!
  • Insets for Frame objects are unusual in that they
    count the menubar (if any).
  • Example
  • public Insets getInsets()
  • return new
  • Insets(10,10,10,10)

22
Using Insets
  • Since components are not placed inside the
    insets, but you can nonetheless draw in the inset
    area, Insets are useful for adding graphic depth
    to layouts
  • They can be used to add border, edging, and the
    like.
  • Suppose we subclass panel to hold components, and
    specify an inset with colored edges . . .

23
  • import java.awt.
  • class MyPanel extends Panel
  • int offset Component c
  • public MyPanel () this (5, null)
  • public MyPanel (Component c)this (5, c)
  • public MyPanel(int offset, Component c)
  • this.offset offset this.cc
  • setLayout (new BorderLayout())
  • if (c!null) add(c, "Center")
  • public Insets getInsets()
  • return new Insets(offset, offset, offset,
    offset)
  • public void paint (Graphics g)
  • Dimension D getSize() Insets I
    getInsets() if (c!null)
  • g.setColor(Color.gray)
    g.fillRect(0,0,D.width, I.top)
    g.fillRect(0,0,I.left, D.height)
    g.fillRect(D.width-I.right, 0, I.right,
    D.height) g.fillRect(0,D.heigh
    t-I.bottom, D.width, D.height)else /WATCH
    THIS SPACE!/

24
Example . . .
  • Thus

25
Introduction to Graphics Objects . . .
  • Every component/container has associated with it
    a graphics object
  • This graphics object can be use to draw on the
    component/container
  • The graphics object is best accessed through the
    paint() method, which is automatically called by
    Java for updates
  • You can also call paint() by requesting
    repaint() at key moments.

26
Graphics and Insets
  • While more specifics about graphics will come
    with the next lecture, we can use some basic
    graphics techniques to improve our panel class.
  • Suppose we find a patter we wish to use as a
    background.
  • Loading the pattern as an image can be costly in
    terms of time and memory.
  • We could instead draw it ourselves . . .

27
Graphics and Insets
  • Find a pattern you like, and consider how you
    might duplicate it in the paint() method

28
Graphics and Insets
  • Our foregoing example breaks down to a few simple
    drawLine calls
  • for (h0 hlt D.height h7)
  • for (w0 wltD.width w7)
    g.drawLine(w,h, w4,h)
    g.drawLine(w4,h, w7,h3)
    g.drawLine(w7,h3,
    w7,h3)
    g.drawLine(w7,h3,w7,h5)
    g.drawLine(w7,h5,w6, h3)
    g.drawLine(w6,h3,
    w6,h6)
    g.drawLine(w6,h6,w6,h6)
    g.drawLine(w6,h6,w5,h3)
    g.drawLine(w5,h3,w1,h7)
    g.drawLine(w1,h7,w
    2,h7)
    g.drawLine(w,h4, w3,h1)
    g.drawLine(w3,h1,w1,h1)

29
Graphics and insets . . .
  • What would happen if we incorporated this pattern
    into our panel, and used null as a sort of
    switch to draw the pattern or the inset borders ?
    . . .

30
  • import java.awt.
  • class MyPanel extends Panel
  • int offset Component c
  • public MyPanel () this (25, null)
  • public MyPanel (Component c) this (5, c)
  • public MyPanel(int offset, Component c)
  • this.offset offset this.cc setLayout (new
    BorderLayout()) if (c!null) add(c, "Center")
  • public Insets getInsets()
  • return new Insets(offset, offset, offset,
    offset)
  • public void paint (Graphics g)
  • Dimension D getSize() Insets I getInsets()
  • if (c!null)
  • g.setColor(Color.gray)
  • g.fillRect(0,0,D.width, I.top)
  • g.fillRect(0,0,I.left, D.height)
  • g.fillRect(D.width-I.right, 0, I.right,D.height)
  • g.fillRect(0,D.height-I.bottom, D.width,
    D.height) else
  • for (int h0 hlt D.height h7)
    for (int w0 wltD.width w7)
  • g.drawLine(w,h, w4,h) g.drawLine(w4,h,
    w7,h3)

31
  • import java.awt.
  • import java.applet.
  • public class test extends Applet
  • public void init()
  • super.init()
  • MyPanel m new MyPanel()
  • m.setLayout(new GridLayout(4,3,5,5))
  • int off-2,2,0 for (int i10 i--gt1) m.add
    (new MyPanel( (new Button (""(ioffi3)))))
  • m.add (new MyPanel ( new Button ("."))) m.add
    (new MyPanel (new Button ("0")))
  • m.add (new MyPanel (new Button ("/-")))
    add(m)
  • public Insets getInsets()
  • return new
  • Insets(5,5,5,5)
Write a Comment
User Comments (0)
About PowerShow.com