lecture13.ppt

advertisement
CS110- Lecture 13
Mar 28, 2005

Announcements
 Exam 2 is next Monday
 It will cover:
 Section 3.9 – 3.11
 Section 4.1 – 4.4
 Section 5.5, Section 5.7 and Section 5.8.
 The exam is closed book but you may bring an 8
.5 X 11 (written on both sides) sheet with any
information you want on it.
 I will post Sample Exam tomorrow.
 We will discuss it on Wednesday and then I will
post its solutions.
6/30/2016
CS110-Spring 2005, Lecture 13
1
Agenda





Nested Panels
Images
Graphical User Interfaces
Buttons
TextFields
6/30/2016
CS110-Spring 2005, Lecture 13
2
Nested Panels

Lets write another class with the
following output.
Nested Panels
Panel One
6/30/2016
Panel two
CS110-Spring 2005, Lecture 13
3
Images



Images often play an important role in
graphics based software.
Java has the ability to use JPEG and GIF
images in various ways.
With the JLabel class, you can display
text, images or both. If you need to
create a component that displays a
string or an image (or both), you can
do so by using or extending JLabel.
6/30/2016
CS110-Spring 2005, Lecture 13
4
Images
 The ImageIcon class is used to represent an



image that is included in a label.
All the label contents have the default
vertical alignment — the label contents are
centered vertically in the label's drawing
area.
We can set the horizontal positioning of the
label within the space.
The orientation of the label’s text and image
is explicitly set using the
setHorizontalTextPosition and
setVerticalTextPosition methods.
6/30/2016
CS110-Spring 2005, Lecture 13
5
The this Reference



The this is a reserved word in java.
It allows an object to refer to itself.
The this reference can be used to refer
to the currently executing object.
public Point(double x1, double y1)
{
x = x1;
y = y1;
}
6/30/2016
CS110-Spring 2005, Lecture 13
6
The this Reference
public Point(double x, double y)
{
this.x = x;
this.y = y;
}
6/30/2016
CS110-Spring 2005, Lecture 13
7
Graphical User Interfaces

At least three kinds of objects are
needed to create a GUI:


Components – an object that defines a
screen element to display information.
Containers is a special type of component
that is used to hold and organize other
components.
Events – an event is an object that
represents some occurrence in which we
may be interested. For e.g. Press of a
mouse button or typing a key on keyboard.
6/30/2016
CS110-Spring 2005, Lecture 13
8
Graphical User Interfaces

Listeners – a listener is an object that
“waits’ for an event to occur and
responds in some way when it does.
We must carefully establish the
relationships among the listener, the
event it listens for, and the component
that will generate the event.
6/30/2016
CS110-Spring 2005, Lecture 13
9
Graphical User Interfaces
Every event handler requires three pieces of code:
 In the declaration for the event handler class, one line of
code specifies that the class implements a listener
interface. For example:
public class MyClass extends JFrame implements
ActionListener {
 Another line of code registers an instance of the event
handler class as a listener on one or more components.
For example: someComponent.addActionListener(this);
 The event handler class has code that implements the
methods in the listener interface. For example:
public void actionPerformed(ActionEvent e) {
...
}
//code that reacts to the action
...
6/30/2016
CS110-Spring 2005, Lecture 13
10
Buttons

Lets write a class for following output.
Push Counter
Push me
6/30/2016
Pushes: 2
CS110-Spring 2005, Lecture 13
11
TextFields

Lets write a class for following output.
Calculator
6/30/2016
First Number
3
Second Number
2
Add
Subtract
Product
Result: 5
Result: 1
Result: 6
CS110-Spring 2005, Lecture 13
12
Download