? 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 1
Chapter 5
Processing Input with Applets
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 2
Chapter 5 Objectives
After you have read and studied this chapter,you
should be able to
Define an applet with multiple methods.
Incorporate a simple event-handling routine to an applet
to process input.
Construct input-processing applets using Label,TextField,
and Button objects from the java.awt package.
Convert string data to numerical data.
Use the reserved word this in your programs.
Run applets without using an applet viewer or browser.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 3
Sample Applet,GreetingApplet
The GreetingApplet applet accepts a name entered
by the user and replies back with a personalized
greeting.
We will develop this applet in two steps:
1,Place GUI components
2,Add an action event handling method
The greeting is displayed
when the user enters the
name and presses the
ENTER key.
This is a text field
where the user
enters his/her name.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 4
Template for an Applet Definition
import java.applet.*;
import java.awt.*;
public class extends Applet
{
}
javadoc
Comment
Applet Name
Methods
Include a sequence
of methods
Declaration
Import
Statements
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 5
GreetingApplet,Placing GUI Objects
public class GreetingApplet extends Applet
{
private Label prompt;
private Label greeting;
private TextField inputLine;
public GreetingApplet( )
{
prompt = new Label(“Please enter your name”);
greeting = new Label( );
inputLine = new TextField( 15 );
add( prompt );
add( greeting );
add( inputLine );
}
}
These statements
place Label and
TextField objects
on this applet.
Declaration
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 6
GreetingApplet,Handling Action Events
We will now add code to process the pressing of the
ENTER key.
Event sources generate or trigger events and Event
listeners process the generated events.
For this applet,the TextField object inputLine is the
event source and GreetingApplet is the event listener.
TextField inputLine generates an action event.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 7
GreetingApplet as an Action Event Handler
To set a GreetingApplet object be an action event
handler,we must
1,Import the Java event-handling classes.
2,Modify the class declaration to include the clause
implements ActionListener.
3,Add the actionPerformed method to the class
definition.
4,Register the GreetingApplet object to the event
source inputLine as its action event listener,(An
event source will call the registered listeners’
actionPerformed method when the event occurs.)
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 8
Template for an Action Processing Applet
import java.applet.*;
import java.awt.*;
import java.event.*;
public class extends Applet
implements ActionListener
{
}
javadoc
Comment
Applet Name
Methods
One of which is
actionPerformed
Import
Statements
Declaration
Implement
Clause
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 9
Registering GreetingApplet
We register an GreetingApplet object as an action
event listener of the event source inputLine by calling
inputLine’s addActionListener method,We do this in
the constructor:
public GreetingApplet
{
//as before
inputLine.addActionListener( this );
}
The reserved word this refers to the GreetingApplet
object,To refer to an object from within the object’s
method,we use the reserved word this.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 10
The actionPerformed Method
public void actionPerformed( ActionEvent
event )
{
greeting.setText(,Nice to meet you,,+
inputLine.getText( ) +,.” );
add( greeting );
doLayout( );
}
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 11
GreetingApplet Object Diagram
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 12
Absolute Positioning of GUI Objects
Placement of GUI objects an applet is control by the
layout manager,Default layout manager for an
applet is FlowLayout.
It is possible to set the layout manager to null
setLayout( null );
and place GUI objects by specifying their position
and size as
<object>,setBounds ( x,y,width,height ) ;
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 13
The setBounds Method
inputLine.setBounds( 65,55,150,
25 );
150
25
55
65
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 14
The Button Class
A type of button called pushbutton is one of most
ubiquitous GUI objects used today’s window-based
programs.
We use the Button class from java.awt to create
pushbuttons.
Button helloButton;
helloButton = new Button(“Hello”);
A button will generate an action event when it is clicked,Register an action event listener to a button
to process the action events.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 15
Converting Text to a Numerical Value
A TextField object we to input data from the user will
accept data as text value.
To input numerical data,we must convert the text
value to a numerical value.
There are Double,Float,Integer,and others that
provide conversion routines.
There is also the Convert class in the javabook
package that provides a consistent way of converting
values,The Convert class uses the Double,Float,
Integer,and other standard classes internally to provide conversion services,Please consult its
reference and source code.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 16
Converting Text to a Double Value
To convert,123.456” to a double value,we write
Doulbe doubleObj = new Double(“123.456”);
double number = doubleObj.doubleValue();
Alternatively,we can write
double number;
number = Double.parseDouble(“123.456”);
Conversion of a text value to other numerical data
types follows the same pattern as above.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 17
Sample Program,Body Mass Index (BMI)
Problem Statement
Write an applet that displays a BMI of a person given his
or her weight in kilograms and height in meters.
Major Tasks
1,Get two input values
2,Convert the input text values to double and compute
the BMI
3,Display the result
Key Formula (given)
BMI = w / h2
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 18
BMIApplet – Design
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 19
Loan Calculator – Object Diagram
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 20
Loan Calculator – Development Steps
1,Start with a skeleton BMIApplet class,The skeleton
class will include data member declarations and a
partially defined constructor that creates and places
GUI objects,
2,Add code to the constructor to create and place all
GUI objects on the applet.
3,Define the actionPerformed method to process the
button click action event,We will include a dummy output statement to verify the button click event is
properly detected.
4,Add code to compute and display the BMI and
finalize the code.