《JAVA OOP开发》英文版 Chapter 5 Processing Input with Applets

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

Greeting Applet: Handling Action Events r We will now add code to process the pressing of the ENTER key r Event sources generate or trigger events and Event listeners process the generated events r For this applet, the TextField object inputLine is the event source and GreetingApplet is the event listener. r TextField inputline generates an action event C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5-6
© 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

GreetingApplet as an Action Event Handler r To set a Greeting Applet 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 Action Listener. 3. Add the action Performed method to the class definition 4. Register the Greeting Applet object to the event source inputline as its action event listener. (An event source will call the registered listeners action Performed method when the event occurs.) C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 5-7
© 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.)

Template for an Action Processing applet import java applet. *i Import import java. awt. *i Statements import java. event. * i avadoc Comment Applet Name public class extends Applet implements ActionListener Implement Clause Declaration Methods One of which is action Performed C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 5-8
© 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

Registering GreetingApplet r 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 )i r 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 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 5-9
© 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

The action Performed method public void actionPerformed( ActionEvent event greeting. setText(Nice to meet you,+ inputLine. getText()+.)i add( greeting )i dolayout()i C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 5-10
© 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( ); }
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《JAVA OOP开发》英文版 Chapter 4 Defining Instantiable Classes.ppt
- 《JAVA OOP开发》英文版 Chapter 3 Numerical Data.ppt
- 《JAVA OOP开发》英文版 Chapter 2 Java Programming Basics.ppt
- 《JAVA OOP开发》英文版 Chapter 1 Introduction to Object-oriented Programming and Software Development.ppt
- 《JAVA OOP开发》英文版 Introduction to Computers and Programming Languages.ppt
- 《Windows DNA应用程式》 面向对象分析与设计讲义.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第四章 第四讲 分支结构.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第十二章 文件.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第十一章 复杂数据类型及排序.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第六讲 数组.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第六讲 数组.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第七章(7-2)指针与指针变量.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第五章 循环结构.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第二章 数据类型与运算符.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第二讲 数据类型与算术运算.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第九讲 函数.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第九章 函数.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第三章 运算符与表达式.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第三讲 运算与顺序结构.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第七章 指针.ppt
- 《JAVA OOP开发》英文版 Chapter 6 Selection statements.ppt
- 《JAVA OOP开发》英文版 Chapter 7 Repetition Statements.ppt
- 《JAVA OOP开发》英文版 Chapter 8 Characters and strings.ppt
- 《JAVA OOP开发》英文版 Chapter 9 objectives.ppt
- 《JAVA OOP开发》英文版 Chapter 10 Sorting and Searching.ppt
- 《JAVA OOP开发》英文版 Chapter 11 File Input and Output.ppt
- 《JAVA OOP开发》英文版 Chapter 12 Reusable classes and packages.ppt
- 《JAVA OOP开发》英文版 Chapter 13 GUI Objects and Event-Driven Programming.ppt
- 《JAVA OOP开发》英文版 Chapter 14 Inheritance and Polymorphism.ppt
- 《JAVA OOP开发》英文版 Chapter 15 Case Study Class Roster Maintenance program.ppt
- 《JAVA OOP开发》英文版 Chapter 16 Chapter 16 Recursive algorithms.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第一章 绪论.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第七章 牛行接与应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第三章 MCS-51单片机指令系统.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第九章 A/D、D/A转换接口.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第二章 MCS-51学机组成理.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第五章 输入/输出与中断.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第八章 并行接口与应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第六章 定时器/计数器及应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第十章 单片机应用系统设计与开发.ppt