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