lecture12.ppt

Download Report

Transcript lecture12.ppt

CS110- Lecture 12
Mar 23, 2005

Agenda
 Questions on Project 2?
 Class dependencies
 Components and Containers
6/30/2016
CS110-Spring 2005, Lecture 12
1
Class dependencies




The classes in a software system have various types
of relationships to each other.
We have seen many examples of “dependency”
relationship in which one class uses another i.e. one
class relies on another in some sense.
Often the methods of one class invokes the methods
of another class.
Example:



ChaosGame is dependent on Die class
MTSO class is dependent on Tower class
Tower class is dependent on Location class
6/30/2016
CS110-Spring 2005, Lecture 12
2
Class Dependencies


Sometimes a class depends on itself i.e.
an object of one class interacts with
another object of the same class.
Example

Location class is dependent on itself
because distance method needs another
Location object. Header of distance
method is:
public double distance(Location other)
{
}
6/30/2016
CS110-Spring 2005, Lecture 12
3
Class dependencies


Lets write a class ComplexNumber.
Instance data:



Real part
Imaginary part
Methods:




sum(ComplexNumber other)
prod(ComplexNumber other)
neg()
Various Setters and Getters
6/30/2016
CS110-Spring 2005, Lecture 12
4
Components and Containers
(Section 3.9)




We have used java capabilities to draw
shapes using the Graphics and Color
classes from standard library.
We wrote applets to draw shapes.
But we can also write applications
(stand – alone programs) with graphical
components.
We need main method to run such
applications.
6/30/2016
CS110-Spring 2005, Lecture 12
5
Components and Containers


A GUI component is an object that
represents a screen element that is
used to display information or allow the
user to interact with the program in a
certain way.
GUI components include labels,
buttons, text fields, radio buttons,
check boxes and so on….
6/30/2016
CS110-Spring 2005, Lecture 12
6
Components and Containers

GUI components and other GUI related
classes are primarily in two packages:

AWT – Abstract windowing toolkit (old)



Swing (New)




Components- Button, Label etc.
Other Classes – Color etc.
Components- JButton, JLabel etc.
Other classes – Timer etc.
We will still use some classes of AWT such as
Color.
But for components we will prefer using
Swing Components.
6/30/2016
CS110-Spring 2005, Lecture 12
7
Components and Containers


A Container is a special type of
Component that is used to hold and
organize other components.
AWT


Containers – Frame, Panel etc.
Swing

Containers – JFrame, JPanel etc.
6/30/2016
CS110-Spring 2005, Lecture 12
8
Frames




Frame is a container that is used to
display GUI based Java applications.
Frame is displayed as a separate
window with its own title bar.
In can be resized, moved, maximized
and minimized.
In swing Frame is defined by the
JFrame class.
6/30/2016
CS110-Spring 2005, Lecture 12
9
Panels



Panel is also a container. However it
cannot be displayed at its own. A panel
must be added to another container for
it to be displayed.
Its primary role is to help organize the
other components in a GUI.
In swing panel is defined by JPanel
class.
6/30/2016
CS110-Spring 2005, Lecture 12
10
Some Fundamental GUI components

Label



Displays a line of text.
In swing label is defined by JLabel class.
Button


Allows user to click at them and we can
associate an action when button is clicked
In swing button is defined by JButton class
6/30/2016
CS110-Spring 2005, Lecture 12
11
Frames and Panels

Lets write a class that gives us the
following output.
Swing
Welcome to world of Swing
Swing is Fun
OK
6/30/2016
EXIT
CS110-Spring 2005, Lecture 12
12