Java Software Solutions Foundations of Program Design
Download
Report
Transcript Java Software Solutions Foundations of Program Design
Java Software Solutions
Lewis and Loftus
Graphics -- Introduction
• The use of graphics is common among modern software
systems
• Java has strong API support for graphics in the
java.awt (abstract windowing toolkit) package
• Chapter 7 focuses on:
–
–
–
–
–
Chapter 7
the coordinate system for Java graphics
the use of color
drawing shapes such as lines, ovals, rectangles, etc.
the use of fonts
basic animation techniques
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
1
Java Software Solutions
Lewis and Loftus
The Graphics Class
• An object of the Graphics class represents a particular
drawing surface
• It defines a graphics context in which drawn shapes will
be rendered
• The Graphics class contains methods for drawing
various shapes and controlling visual aspects like font
and color
• An applet has a graphics context, which is automatically
passed to the paint method when it is called
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
2
Java Software Solutions
Lewis and Loftus
The Coordinate System
• A simple two-dimensional coordinate system exists for
each graphics context (or drawing surface)
• Each point on the coordinate system represents a single
pixel
• The top left corner of the area is coordinate <0, 0>
• A drawing surface has a particular width and height
• Anything drawn outside of that area will not be visible
• See Coordinates.java
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
3
Java Software Solutions
Lewis and Loftus
The Coordinate System
<0, 0>
X
x
y
<x, y>
<width-1, height-1>
Y
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
4
Java Software Solutions
Lewis and Loftus
Color
• The Color class is used to define and manage the color
in which shapes are drawn
• Colors are defined by their RGB value, which defines the
relative contribution of the primary colors red, green, and
blue
• Each drawing surface has a foreground color and a
background color
• The setColor method of the Graphics class defines
the foreground color, and the setBackground method
of the component (the applet) sets the background color
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
5
Java Software Solutions
Lewis and Loftus
Color
• The Color class contains several predefined colors,
defined as public, static constants
• See Nature.java
• Many other colors can be defined using the constructor
of the Color class
• Over 16 million colors can be defined, but humans
cannot distinguish between many similar colors
• Furthermore, the hardware of most systems has
limitations to the color options available
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
6
Java Software Solutions
Lewis and Loftus
XOR Mode
• Drawing in normal mode causes shapes of the same color
to blend together
• Drawing in XOR mode causes the overlapping portions
of the shapes to be rendered in a contrasting color
• This effect can be used to "erase" a shape by redrawing it
in the same color in the same spot while in XOR mode
• See XOR_Demo.java
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
7
Java Software Solutions
Lewis and Loftus
Drawing Shapes
• The Graphics class contains methods for drawing
several specific shapes:
lines, ovals, rectangles, arcs, polygons, and polylines
• Most shapes can be drawn filled or unfilled
• A line, drawn with the drawLine method, is always
one pixel wide and cannot be filled
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
8
Java Software Solutions
Lewis and Loftus
Ovals
• An oval is defined by its bounding rectangle:
width
height
• The methods that draw an oval take four parameters, all
integers:
drawOval (x, y, width, height)
fillOval (x, y, width, height)
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
9
Java Software Solutions
Lewis and Loftus
Ovals
• The first two parameters are the <x, y> coordinate of the
top-left corner of the bounding rectangle
• The third and fourth parameters specify the width and
height of the bounding rectangle
• The drawOval method draws an unfilled oval and the
fillOval method draws a filled (opaque) oval
• See Ovals.java and Rotating_Disk.java
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
10
Java Software Solutions
Lewis and Loftus
Rectangles
• Rectangles can be drawn
– filled or unfilled
– with squared or rounded corners
– with a slight three-dimensional effect or not
• The primary parameters for all rectangle drawing
methods define the upper left corner of the rectangle and
its width and height
• The shape of the rounded corner of a rounded rectangle
are defined by an arc width and height
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
11
Java Software Solutions
Lewis and Loftus
Rectangles
• A three dimensional rectangle is shown using a small
highlight on two sides of the rectangle
• The highlight appears on the bottom and right or the top
and left as specified by a boolean parameter to the 3D
drawing methods
• See Rectangles.java,
Rounded_Rectangles.java, and
Three_D_Rectangles.java
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
12
Java Software Solutions
Lewis and Loftus
Arcs
• An arc is defined as a segment of an oval
• The first four parameters to the arc drawing methods
define the bounding rectangle of the oval (top left corner,
width, and height)
• The other two parameters define the start angle and the
arc angle
• The start angle indicates where the arc begins and the arc
angle determines how far the arc sweeps across its
defining oval
• See Arc.java
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
13
Java Software Solutions
Lewis and Loftus
Arcs
• The start angle can be specified using both positive and
negative values:
90
-270
45
-315
0
360
-360
180
-180
270
-90
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
14
Java Software Solutions
Lewis and Loftus
Arcs
• An arc angle can also be positive or negative
• A positive arc angle sweeps counterclockwise, and a
negative arc angle sweeps clockwise
• Therefore, the same arc can be specified using four
different combinations of start and arc angles
• Arcs can also be filled or unfilled
• See Arcs.java
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
15
Java Software Solutions
Lewis and Loftus
Polygons
• A polygon is a multisided figure defined by a series of
ordered points
• Line segments connecting the points form the polygon
• The points are defined by corresponding arrays of x and
y coordinate values, and can already be incorporated into
an object of the Polygon class
• Polygons are closed, forming a line segment from the last
point back to the first
• See Polygons.java
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
16
Java Software Solutions
Lewis and Loftus
Polylines
• A polyline is similar to a polygon except that it is not
closed
• That is, there is no line segment from the last point back
to the first unless explicitly specified
• They are convenient for specifying certain kinds of
complex shapes
• Polylines cannot be filled
• See Polylines.java
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
17
Java Software Solutions
Lewis and Loftus
Fonts
• A font defines the look of each character when it is
printed or drawn
• The Font class provides methods for specifying fonts in
a Java program
• Each computer system supports a specific set of fonts
• See Font_Lister.java
• The setFont method defines the current font for a
program
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
18
Java Software Solutions
Lewis and Loftus
Fonts
• A font is defined using the Font class constructor and a
combination of:
– font name
– font style: plain, bold, italic, or bold+italic
– font size, in points
• Constants are defined in the Font class to specify the
font style
• See Entropy.java
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
19
Java Software Solutions
Lewis and Loftus
Animations
• Simple animations can be accomplished by drawing a
shape, then erasing it, then drawing it again in a slightly
altered position
• Erasing can be accomplished through careful use of
XOR mode
• Timing must be controlled so that the animation does not
move too fast
• See Bouncing_Ball.java
Chapter 7
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
20
Java Software Solutions
Lewis and Loftus
Inheritance -- Introduction
• Another fundamental object-oriented technique is called
inheritance, which, when used correctly, supports reuse
and enhances software designs
• Chapter 8 focuses on:
– the concept of inheritance
– inheritance in Java
– the protected modifier
– adding and modifying methods through inheritance
– creating class hierarchies
Chapter 8
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
1
Java Software Solutions
Lewis and Loftus
Inheritance
• Inheritance allows a software developer to derive a new
class from an existing one
• The existing class is called the parent class, or
superclass, or base class
• The derived class is called the child class or subclass.
• As the name implies, the child inherits characteristics of
the parent
• In programming, the child class inherits the methods and
data defined for the parent class
Chapter 8
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
2
Java Software Solutions
Lewis and Loftus
Inheritance
• Inheritance relationships are often shown graphically,
with the arrow pointing to the parent class:
Vehicle
Car
• Inheritance should create an is-a relationship, meaning
the child is-a more specific version of the parent
Chapter 8
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
3
Java Software Solutions
Lewis and Loftus
Deriving Subclasses
• In Java, the reserved word extends is used to establish
an inheritance relationship
class Car extends Vehicle {
// class contents
}
• See Words.java
Chapter 8
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
4
Java Software Solutions
Lewis and Loftus
The protected Modifier
• The visibility modifiers determine which class members
get inherited and which do not
• Variables and methods declared with public visibility
are inherited, and those with private visibility are not
• But public variables violate our goal of encapsulation
• The protected visibility modifier allows a member to
be inherited, but provides more protection than public
does
• The details of each modifier are given in Appendix F
Chapter 8
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
5
Java Software Solutions
Lewis and Loftus
The super Reference
• Constructors are not inherited, even though they have
public visibility
• Yet we often want to use the parent's constructor to set
up the "parent's part" of the object
• The super reference can be used to refer to the parent
class, and is often used to invoke the parent's constructor
• See Words2.java
Chapter 8
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
6
Java Software Solutions
Lewis and Loftus
Defined vs. Inherited
• A subtle feature of inheritance is the fact that even if a
method or variable is not inherited by a child, it is still
defined for that child
• An inherited member can be referenced directly in the
child class, as if it were declared in the child class
• But even members that are not inherited exist for the
child, and can be referenced indirectly through parent
methods
• See Eating.java and School.java
Chapter 8
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
7
Java Software Solutions
Lewis and Loftus
Overriding Methods
• A child class can override the definition of an inherited
method in favor of its own
• That is, a child can redefine a method it inherits from its
parent
• The new method must have the same signature as the
parent's method, but can have different code in the body
• The object type determines which method is invoked
• See Messages.java
Chapter 8
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
8
Java Software Solutions
Lewis and Loftus
Overloading vs. Overriding
• Don't confuse the concepts of overloading and overriding
• Overloading deals with multiple methods in the same
class with the same name but different signatures
• Overriding deals with two methods, one in a parent class
and one in a child class, that have the same signature
• Overloading lets you define a similar operation in
different ways for different data
• Overriding lets you define a similar operation in different
ways for different object types
Chapter 8
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
9
Java Software Solutions
Lewis and Loftus
The super Reference Revisited
• The super reference can be used to invoke any method
from the parent class
• This ability is often helpful when using overridden
methods
• The syntax is:
super.method(parameters)
• See Firm.java and Accounts.java
Chapter 8
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
10
Java Software Solutions
Lewis and Loftus
Class Hierarchies
• A child class of one parent can be the parent of another
child, forming class hierarchies:
Business
Retail_Business
Macy's
Chapter 8
Service_Business
K-Mart
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
Kinko's
11
Java Software Solutions
Lewis and Loftus
Class Hierarchies
• Two children of the same parent are called siblings
• Good class design puts all common features as high in
the hierarchy as is reasonable
• Class hierarchies often have to be extended and modified
to keep up with changing needs
• There is no single class hierarchy that is appropriate for
all situations
• See Accounts2.java
Chapter 8
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
12
Java Software Solutions
Lewis and Loftus
The Object Class
• All objects are derived from the Object class
• If a class is not explicitly defined to be the child of an
existing class, it is assumed to be the child of the
Object class
• The Object class is therefore the ultimate root of all
class hierarchies
• The Object class contains a few useful methods, such
as toString(), which are inherited by all classes
• See Test_toString.java
Chapter 8
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
13
Java Software Solutions
Lewis and Loftus
References and Inheritance
• An object reference can refer to an object of its class, or
to an object of any class related to it by inheritance
• For example, if the Holiday class is used to derive a
child class called Christmas, then a Holiday
reference could actually be used to point to a
Christmas object:
Holiday day;
day = new Christmas();
Chapter 8
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
14
Java Software Solutions
Lewis and Loftus
References and Inheritance
• Assigning a predecessor object to an ancestor reference
is considered to be a widening conversion, and can be
performed by simple assignment
• Assigning an ancestor object to a predecessor reference
can also be done, but it is considered to be a narrowing
conversion and must be done with a cast
• The widening conversion is the most useful
Chapter 8
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
15
Java Software Solutions
Lewis and Loftus
Polymorphism
• A polymorphic reference is one which can refer to one of
several possible methods
• Suppose the Holiday class has a method called
celebrate, and the Christmas class overrode it
• Now consider the following invocation:
day.celebrate();
• If day refers to a Holiday object, it invokes
Holiday's version of celebrate; if it refers to a
Christmas object, it invokes that version
Chapter 8
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
16
Java Software Solutions
Lewis and Loftus
Polymorphism
• In general, it is the type of the object being referenced,
not the reference type, that determines which method is
invoked
• See Messages2.java
• Note that, if an invocation is in a loop, the exact same
line of code could execute different methods at different
times
• Polymorphic references are therefore resolved at runtime, not during compilation
Chapter 8
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
17
Java Software Solutions
Lewis and Loftus
Polymorphism
• Note that, because all classes inherit from the Object
class, an Object reference can refer to any type of
object
• A Vector is designed to store Object references
• The instanceOf operator can be used to determine the
class from which an object was created
• See Variety.java
Chapter 8
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
18
Java Software Solutions
Lewis and Loftus
Polymorphism
• See Firm2.java
Staff_Member
Employee
Hourly
Chapter 8
Volunteer
Executive
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
19
Java Software Solutions
Lewis and Loftus
Enhanced Class Design -- Introduction
• We now examine several features of class design and
organization that can improve reusability and system
elegance
• Chapter 9 focuses on:
– abstract classes
– formal Java interfaces
– packages
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
1
Java Software Solutions
Lewis and Loftus
Abstract Classes
• An abstract class cannot be instantiated
• It is used in a class hierarchy to organize common
features at appropriate levels
• An abstract method has no implementation, just a name
and signature
• An abstract class often contains abstract methods
• Any class that contains an abstract method is by
definition abstract
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
2
Java Software Solutions
Lewis and Loftus
Abstract Classes
• The modifier abstract is used to define abstract
classes and methods
• The children of the abstract class are expected to define
implementations for the abstract methods in ways
appropriate for them
• If a child class does not define all abstract methods of the
parent, then the child is also abstract
• An abstract class is often too generic to be of use by
itself
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
3
Java Software Solutions
Lewis and Loftus
Abstract Classes
• See Dinner.java
Food
Beans
Chapter 9
Franks
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
Pepperoni
4
Java Software Solutions
Lewis and Loftus
Abstract Classes
• See Printer.java
File
Binary_File
Text_File
Image_File
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
5
Java Software Solutions
Lewis and Loftus
Abstract Classes
• An abstract method cannot be declared as final,
because it must be overridden in a child class
• An abstract method cannot be declared as static,
because it cannot be invoked without an implementation
• Abstract classes are placeholders that help organize
information and provide a base for polymorphic
references
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
6
Java Software Solutions
Lewis and Loftus
Interfaces
• We've used the term interface to mean the set of service
methods provided by an object
• That is, the set of methods that can be invoked through
an object define the way the rest of the system interacts,
or interfaces, with that object
• The Java language has an interface construct that
formalizes this concept
• A Java interface is a collection of constants and abstract
methods
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
7
Java Software Solutions
Lewis and Loftus
Interfaces
• A class that implements an interface must provide
implementations for all of the methods defined in the
interface
• This relationship is specified in the header of the class:
class class-name implements interface-name {
}
• See Soap_Box.java
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
8
Java Software Solutions
Lewis and Loftus
Interfaces
• An interface can be implemented by multiple classes
• Each implementing class can provide their own unique
version of the method definitions
• An interface is not a class, and cannot be used to
instantiate an object
• An interface is not part of the class hierarchy
• A class can be derived from a base class and implement
one or more interfaces
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
9
Java Software Solutions
Lewis and Loftus
Interfaces
• Unlike interface methods, interface constants require
nothing special of the implementing class
• Constants in an interface can be used in the
implementing class as if they were declared locally
• This feature provides a convenient technique for
distributing common constant values among multiple
classes
• See File_Protection.java
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
10
Java Software Solutions
Lewis and Loftus
Interfaces
• An interface can be derived from another interface, using
the extends reserved word
• The child interface inherits the constants and abstract
methods of the parent
• Note that the interface hierarchy and the class hierarchy
are distinct
• A class that implements the child interface must define
all methods in both the parent and child
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
11
Java Software Solutions
Lewis and Loftus
Interfaces
• An interface name can be used as a generic reference
type name
• A reference to any object of any class that implements
that interface is compatible with that type
• For example, if Philosopher is the name of an
interface, it can be used as the type of a parameter to a
method
• An object of any class that implements Philosopher
can be passed to that method
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
12
Java Software Solutions
Lewis and Loftus
Interfaces
• Note the similarities between interfaces and abstract
classes
• Both define abstract methods that are given definitions
by a particular class
• Both can be used as generic type names for references
• However, a class can implement multiple interfaces, but
can only be derived from one class
• See Printer2.java
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
13
Java Software Solutions
Lewis and Loftus
Interfaces
• A class that implements multiple interfaces specifies all
of them in its header, separated by commas
• The ability to implement multiple interfaces provides
many of the features of multiple inheritance, the ability
to derive one class from two or more parents
• Java does not support multiple inheritance
• See Readable_Files.java
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
14
Java Software Solutions
Lewis and Loftus
Packages
• A Java package is a collection of classes
• The classes in a package may or may not be related by
inheritance
• A package is used to group similar and interdependent
classes together
• The Java API is composed of multiple packages
• The import statement is used to assert that a particular
program will use classes from a particular package
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
15
Java Software Solutions
Lewis and Loftus
Packages
• A programmer can define a package and add classes to it
• The package statement is used to specify that all classes
defined in a file belong to a particular package
• The syntax of the package statement is:
package package-name;
• It must be located at the top of a file, and there can be
only one package statement per file
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
16
Java Software Solutions
Lewis and Loftus
Packages
• The classes must be organized in the directory structure
such that they can be found when referenced by an
import statement
• There is a CLASSPATH environment variable on each
computer system that determines where to look for
classes when referenced
• See Simple_IO_Test.java
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
17
Java Software Solutions
Lewis and Loftus
Packages
• The import statement specifies particular classes, or an
entire package of classes, that can be used in that
program
• Import statements are not necessary; a class can always
be referenced by its fully qualified name in-line
• See Simple_IO_Test2.java
• If two classes from two packages have the same name
and are used in the same program, they must be
referenced by their fully qualified name
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
18
Java Software Solutions
Lewis and Loftus
Packages
• As a rule of thumb, if you will use only one class from a
package, import that class specifically
• See Simple_IO_Test3.java
• If two or more classes will be used, use the * wildcard
character in the import statement to provide access to all
classes in the package
Chapter 9
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
19
Java Software Solutions
Lewis and Loftus
Graphical User Interfaces -- Introduction
• Users have become accustomed to using a graphical user
interface (GUI) through which they interact with a
program
• Java provides strong support for building GUIs through
the java.awt package
• Chapter 10 focuses on:
–
–
–
–
Chapter 10
GUI components
event-driven programming
containers and component hierarchies
layout managers
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
1
Java Software Solutions
Lewis and Loftus
GUI Elements
• The key elements of a Java graphical user interface are:
– GUI components
– layout managers
– event processing
• GUI components, such as text fields and buttons, are the
screen elements that a user manipulates with the mouse
and keyboard
• Layout managers govern how the components appear on
the screen
• Events signal important user actions, like a mouse click
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
2
Java Software Solutions
Lewis and Loftus
Event-Driven Programming
• Programs with GUIs must respond to events, generated
by GUI components, that indicate that specific actions
have occurred
• A special category of classes, called listeners, wait for
events to occur
• Therefore, a GUI program is composed of:
– the code that presents the GUI to the user
– the listeners that wait for events to occur
– the specific code that is executed when events occur
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
3
Java Software Solutions
Lewis and Loftus
Event-Driven Programming
• There is a listener interface defined for each event type
• Each listener interface contains the abstract methods
required to respond to specific events
• A listener class implements a particular listener interface
• Listeners are "added" to a particular GUI component
• When a component generates an event, the method
corresponding to that event is executed in the listener
• See Mimic.java
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
4
Java Software Solutions
Lewis and Loftus
The GUI Program Model
Listeners
Handle events
Add listeners
GUI
Event effects
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
Programspecific
5
Java Software Solutions
Lewis and Loftus
Event Interfaces
• Multiple listeners can be added to a component
• Multiple components can be processed by the same
listener
• Furthermore, one listener class can implement multiple
listener interfaces
• Therefore one class can listen for many types of events
• See Events.java
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
6
Java Software Solutions
Lewis and Loftus
Containers
• A container is a special category of GUI components that
group other components
• All containers are components, but not all components
are containers
• An applet is a container
• Therefore, buttons, text fields, and other components can
be added to an applet to be displayed
• Each container has an associated layout manager to
control the way components in it are displayed
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
7
Java Software Solutions
Lewis and Loftus
Containers
• Some containers must be attached to another graphical
surface:
– panel
– applet
• An applet is attached to a browser or appletviewer
window
• Other containers can be moved independently:
– window
– frame
– dialog
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
8
Java Software Solutions
Lewis and Loftus
Containers
Component
Container
Window
Panel
Applet
Chapter 10
Frame
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
Dialog
9
Java Software Solutions
Lewis and Loftus
Component Hierarchies
• A GUI is created when containers and other components
are put together
• The relationships between these components form a
component hierarchy
• For example, an applet can contain panels which contain
other panels which contain buttons, etc.
• See Rings_Display.java
• Careful design of the component hierarchy is important
for visually pleasing and consistent GUIs
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
10
Java Software Solutions
Lewis and Loftus
GUI Components
• There are several GUI components that permit specific
kinds of user interaction:
–
–
–
–
–
–
Chapter 10
labels
text fields
text areas
lists
buttons
scrollbars
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
11
Java Software Solutions
Lewis and Loftus
Labels
• A label defines a line of text displayed on a GUI
• Labels are static in the sense that they cannot be selected
or modified by the human user once added to a container
• A label is instantiated from the Label class
• The Label class contains several constructors and
methods for setting up and modifying a label's content
and alignment
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
12
Java Software Solutions
Lewis and Loftus
Text Fields and Text Areas
• A text field displays a single line of text in a GUI
• It can be made editable, and provide a means to get input
from the user
• A text area is similar, but displays multiple lines of text
• They are defined by the TextField and TextArea
classes
• A text area automatically has scrollbars on its bottom and
right sides
• See Fahrenheit.java
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
13
Java Software Solutions
Lewis and Loftus
Lists
• A list, in the Java GUI sense, is used to display a list
selectable strings
• A list component can contain any number of strings and
can be instantiated to allow multiple selections within it
• The size of the list is specified by the number of visible
rows or strings within it
• A scrollbar will automatically appear on the right side of
a list if the number of items exceed the visible area
• A list is defined by the List class
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
14
Java Software Solutions
Lewis and Loftus
Buttons
• The java.awt package supports four distinct types of
buttons:
–
–
–
–
Push buttons
Choice Buttons
Checkbox buttons
Radio buttons
• Each button type serves a particular purpose
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
15
Java Software Solutions
Lewis and Loftus
Push Button
• A push button is a single button which can be created
with or without a label
• A system is usually designed such that when a push
button is pressed, a particular action occurs
• It is defined by the Button class
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
16
Java Software Solutions
Lewis and Loftus
Choice button
• A choice button is a single button which displays a list of
choices when pushed
• The user can then scroll through and choose the
appropriate option
• The current choice is displayed next to the choice button
• It is defined by the Choice class
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
17
Java Software Solutions
Lewis and Loftus
Checkbox button
• A checkbox button can be toggled on or off
• A set of checkbox buttons are often used to define a set
of options as a group, though one can be used by itself
• If used in a group, more than one option can be chosen at
any one time
• Defined by the Checkbox class
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
18
Java Software Solutions
Lewis and Loftus
Radio buttons
• A radio button, like a checkbox button, is toggled on or
off
• Radio buttons must be grouped into a set, and only one
button can be selected at any one time
• When one button of a group is selected, the currently
selected button in that group is automatically reset
• They are used to select among a set of mutually
exclusive options
• Radio button sets are defined by the Checkbox and
CheckboxGroup classes
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
19
Java Software Solutions
Lewis and Loftus
Scrollbars
• A scrollbar is a slider that indicates a relative position or
quantity
• They are automatic on text areas and list components, but
can be used independently
• The position of the slider in the range corresponds to a
particular numeric value in a range associated with the
scrollbar
• A scrollbar is defined by the Scrollbar class
• See Zoom.java
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
20
Java Software Solutions
Lewis and Loftus
Layout Managers
• There are five predefined layout managers in the
java.awt package:
–
–
–
–
–
flow layout
border layout
card layout
grid layout
grid bag layout
• Each container has a particular layout manager
associated with it by default
• A programmer can also create custom layout managers
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
21
Java Software Solutions
Lewis and Loftus
Flow Layout
• Components are placed in a row from left to right in the
order in which they are added
• A new row is started when no more components can fit
in the current row
• The components are centered in each row by default
• The programmer can specify the size of both the vertical
and horizontal gaps between the components
• Flow layout is the default layout for panels and applets
• See Flow.java
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
22
Java Software Solutions
Lewis and Loftus
Grid Layout
• Components are placed in a grid with a user-specified
number of columns and rows
• Each component occupies exactly one grid cell
• Grid cells are filled left to right and top to bottom
• All cells in the grid are the same size
• See Grid.java
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
23
Java Software Solutions
Lewis and Loftus
Border Layout
• Defines five locations each of which a component or
components can be added
– North, South, East, West, and Center
• The programmer specifies the area in which a component
should appear
• The relative dimensions of the areas are governed by the
size of the components added to them
• See Border.java
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
24
Java Software Solutions
Lewis and Loftus
Border Layout
North
West
Center
East
South
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
25
Java Software Solutions
Lewis and Loftus
Card Layout
• Components governed by a card layout are "stacked"
such that only one component is displayed on the screen
at any one time
• Components are ordered according to the order in which
they were added to the container
• Methods control which component is currently visible in
the container
• See Card.java
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
26
Java Software Solutions
Lewis and Loftus
Grid Bag Layout
• Designed as a two-dimensional grid of columns and rows
• However, not all cells in the grid are the same size
• Components may span multiple columns and rows
• Each component in a grid bag layout is associated with a
set of constraints, defined by the
GridBagConstraints class
• A grid bag layout is the most versatile, and most
complex, of the predefined layout managers
• See Grid_Bag.java
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
27
Java Software Solutions
Lewis and Loftus
GUI Design
• Careful design of a graphical user interface is key to a
viable software system
• To the user, the user interface is the system
• For each situation, consider which components are best
suited and how they should best be arranged
• See Quotes.java
Chapter 10
Copyright 1997 by John Lewis and William Loftus. All rights reserved.
28