Chapter 5 Processing Input with Applets 2000 McGraw-Hl‖ Introduction to Object-Oriented Programming with Java-Wu Chapter 5-1
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5 - 1 Chapter 5 Processing Input with Applets
Chapter 5 objectives After you have read and studied this chapter, you should be able to e Define an applet with multiple methods e Incorporate a simple event-handling routine to an applet to process input. e Construct input-processing applets using Label, TextField, and Button objects from the java. awt package e Convert string data to numerical data e Use the reserved word this in your programs e Run applets without using an applet viewer or browser. C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5-2
© 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
Sample applet: GreetingApplet r The GreetingApplet applet accepts a name entered y the user and replies back with a personalized b greeting Applet viewer: Greeting Applet class回区 This is a text field Please enter your name Mireille where the user Nice to meet you, mireille enters his/her name The greeting is displayed when the user enters the name and presses the pple started ENTER key r We will develop this applet in two steps 1. Place GUi components 2. Add an action event handling method C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 5-3
© 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
Template for an applet definition import java applet. *i Import import java. awt. *i Statements Ja vado Comment public class extends Applet Applet Name Declaration Method Include a sequence of methods C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 5-4
© 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
GreetingApplet: Placing GUi objects public class GreetingApplet extends Applet private label prompt private label greeting; Declaration private TextField inputlinei public GreetingApplet( prompt new Label (Please enter your name")i greeting new Label( )i inputline new TextField( 15 )i add( prompt )i These statements add( greeting place Label and add( inputline )i TextField objects on this applet C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5-5
© 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