复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 12 Event-Driven Programming

Chapter 12 Event-Driven Programming Prerequisi七es王 or part工工工 Chapter 8 Inheritance and Polymorphism Chapter 9 Abstract Classes and Interfaces Chapter 11 Getting Started with GUI Programming hapter 12 Event-Driven Programming 找出画中真谛 一保罗塞尚 hapter 13 Creating User Interfaces Chapter 14 Applets, Images, Audio Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 12 Event-Driven Programming Prerequisites for Part III Chapter 11 Getting Started with GUI Programming Chapter 8 Inheritance and Polymorphism Chapter 13 Creating User Interfaces Chapter 14 Applets, Images, Audio Chapter 9 Abstract Classes and Interfaces Chapter 12 Event-Driven Programming 找出画中真谛 — 保罗.塞尚

Objectives o To explain the concept of event-driven programming($ 12.2) o To understand event, event source, and event classes(8 12.2) To declare listener classes and write the code to handle events (§11.3) o To register listener objects in the source object(8 11.3) ● To understand how an event is handled(§113) ● To write programs to deal with△ Actionevent(§11.3) To write programs to deal with Mouse Event(8 11.4) ● To write programs to deal with Key Event(§115) o To use the Timer class to control animations(8 11.6 Optional) Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 2 Objectives ⚫ To explain the concept of event-driven programming (§12.2). ⚫ To understand event, event source, and event classes (§12.2). ⚫ To declare listener classes and write the code to handle events (§11.3). ⚫ To register listener objects in the source object (§11.3). ⚫ To understand how an event is handled (§11.3). ⚫ To write programs to deal with ActionEvent (§11.3). ⚫ To write programs to deal with MouseEvent (§11.4). ⚫ To write programs to deal with KeyEvent (§11.5). ⚫ To use the Timer class to control animations (§11.6 Optional)

Procedural ys. Event-Driven Programming Procedural programming is executed in procedural order o In oo gui programming, code is executed upon activation of events ● GUIs are event driven O Generate events when user interacts with GUI Oe. g, moving mouse, pressing button, typing in text field, etc o Class java. awt. AWTEvent Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 3 Procedural vs. Event-Driven Programming ⚫ Procedural programming is executed in procedural order. ⚫ In OO GUI programming, code is executed upon activation of events. ⚫ GUIs are event driven Generate events when user interacts with GUI ⚫e.g., moving mouse, pressing button, typing in text field, etc. ⚫Class java.awt.AWTEvent

Events o An event can be defined as a type of signal to the program that something has happened o The event is generated by external user actions such as mouse movements. mouse clicks, and keystrokes, or by the operating system, such as a timer Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 4 Events ⚫An event can be defined as a type of signal to the program that something has happened. ⚫The event is generated by external user actions such as mouse movements, mouse clicks, and keystrokes, or by the operating system, such as a timer

Event classes Action Event Container event Adjustment Event Focus Event Mouse event entObject AWTEvent Component Event InputEvent ItemEvent Paint event Key event Text event WindowEvent ListSelection Event Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 5 Event Classes EventObject AWTEvent AdjustmentEvent ComponentEvent TextEvent ItemEvent ActionEvent InputEvent WindowEvent MouseEvent KeyEvent ContainerEvent FocusEvent PaintEvent ListSelectionEvent

Event Information o An event object contains whatever properties are pertinent to the event You can identify the source object of the event using the getsourcen instance method in the eventobiect class o The subclasses of Eventobject deal with special types of events such as button actions window events component events, mouse movements, and keystrokes. Table 12.1 lists external user actions, source objects, and event types generated Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 6 Event Information ⚫An event object contains whatever properties are pertinent to the event. ⚫You can identify the source object of the event using the getSource() instance method in the EventObject class. ⚫The subclasses of EventObject deal with special types of events, such as button actions, window events, component events, mouse movements, and keystrokes. Table 12.1 lists external user actions, source objects, and event types generated

Selected user actions Event Type User action Object Generated Click a button JBut七。n Ac七i。 eVent Click a check box CHeckbOx 工 temEvent, Actioneven七 Click a radio button Radi。 Button ItemEvent ActionEvent Press return on a text field JTextField Ac七i。 nEven Select a new item JComboBox 工七 emEter七, Actionevent Window opened, closed, etc. W主ndow W主 ndowEvent Mouse pressed, released, etc Component Mouseevent Key released, pressed, etc Component KeyEvent Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 7 Selected User Actions Source Event Type User Action Object Generated Click a button JButton ActionEvent Click a check box JCheckBox ItemEvent, ActionEvent Click a radio button JRadioButton ItemEvent, ActionEvent Press return on a text field JTextField ActionEvent Select a new item JComboBox ItemEvent, ActionEvent Window opened, closed, etc. Window WindowEvent Mouse pressed, released, etc. Component MouseEvent Key released, pressed, etc. Component KeyEvent

Event-Handling model ● Event-handling model O Three parts ● Event source GUi component with which user interacts ● Event object Encapsulates information about event that occurred ● Event listener Receives event object when notified, then responds O Programmer must perform two tasks o Register event listener for event source Implement event-handling method (event handler Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 8 Event-Handling Model ⚫ Event-handling model Three parts ⚫Event source • GUI component with which user interacts ⚫Event object • Encapsulates information about event that occurred ⚫Event listener • Receives event object when notified, then responds Programmer must perform two tasks ⚫Register event listener for event source ⚫Implement event-handling method (event handler)

The Delegation model Register by invoking Trigger an event source. addXlistener(listener); User source: sourceclas listener: Listener Class Action +addXListener(LIstener listener) Keep it a list LIstener event: XEvent listener 1 Invoke listener 2 listener l handler(event) handler(XEvent event istener2. handler(event) histenern stenen handler(event) Internal function of the source object Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 9 The Delegation Model source: SourceClass +addXListener(XListener listener) User listener: ListenerClass Action Trigger an event XListener +handler(XEvent event) Internal function of the source object event: XEvent listener1 listener2 … listenern +handler( XEvent Register by invoking source.addXListener(listener); Keep it a list Invoke listener1.handler(event) listener2.handler(event) … listenern.handler(event)

How Event Handling works o Two open questions from OHow did event handler get registered? ● Answer Through components method addAction Listener OHow does component know to call action Performed? ● Answer: Event is dispatched only to listeners of appropriate type Each event type has corresponding event-listener interface Event ID specifies event type that occurred For example: Mouse Event event's ID spedify which method of the seven events will be handler Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 10 How Event Handling Works ⚫Two open questions from How did event handler get registered? ⚫Answer: • Through component’s method addActionListener How does component know to call actionPerformed? ⚫Answer: • Event is dispatched only to listeners of appropriate type • Each event type has corresponding event-listener interface Event ID specifies event type that occurred For example: MouseEvent event’s ID spedify which method of the seven events will be handler
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 11 Getting Started with GUI Programming.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 10 Object-Oriented Modeling(oom).ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 9 Abstract Classes and Interfaces.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 8 Inheritance and Polymorphism.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 7 String.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 6 Objects and Classes.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 5 Arrays.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 4 Methods.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 3 Control Statements.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 2 Primitive Data Types and Operations.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 1 Introduction to Computers, Programs, and Java.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 0 course intro Programming Language(Using Java).ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)Game of Life_题目要求.pdf
- 复旦大学:《程序设计》课程教学资源(Java Lab)Game of Life.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)验证哥德巴赫猜想.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)Ansi Temple.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)富人Smith的生日 If else Switch.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)3.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)环境配置与第一个程序的运行.ppt
- 复旦大学:《程序设计》课程教学资源(Java Lab)第一个java程序.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 13 Creating User Interfaces.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 14 Applets, Images, and Audio.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 15 Exceptions and Assertions.ppt
- 复旦大学:《程序设计》课程教学资源(PPT课件)Chapter 16 Simple Input and Output.ppt
- 复旦大学:《信息安全》教学课件_01 Classical Encryption Techniques.pdf
- 复旦大学:《信息安全》教学课件_02 Classical Encryption Techniques(cont.).pdf
- 复旦大学:《信息安全》教学课件_03 Modern Block Ciphers.pdf
- 复旦大学:《信息安全》教学课件_04 Public Key Cryptography, RSA.pdf
- 复旦大学:《信息安全》教学课件_05 Message authentication and Hash function.pdf
- 复旦大学:《信息安全》教学课件_06 The Intro to Information Security.pdf
- 复旦大学:《信息安全》教学课件_07-08 Public Key Infrastructure(PKI)公钥基础设施——公钥技术的应用.pdf
- 复旦大学:《信息安全》教学课件_09 Authentication and supplements.pdf
- 复旦大学:《信息安全》教学课件_10 Authentication Kerberos.pdf
- 复旦大学:《信息安全》教学课件_11.1 IP Security.pdf
- 复旦大学:《信息安全》教学课件_11.2 Web & EC Security.pdf
- 复旦大学:《信息安全》教学课件_12-13 Software Security.pdf
- 复旦大学:《信息安全》教学课件_14 防火墙 Firewall.pdf
- 复旦大学:《信息安全原理》课程教学资源(PPT课件)第1章 信息系统安全概述、第2章 密码学概论(1/3).pdf
- 复旦大学:《信息安全原理》课程教学资源(PPT课件)第2章 密码学概论(2/3).pdf
- 复旦大学:《信息安全原理》课程教学资源(PPT课件)第3章 现代加密算法(2/3).pdf