Event-Driven Programming 2011.04.08. Outline Event and Event Source Event Types Listeners,Registerations,and Handling Events Event’s Declaration.

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
JAVA 程式語言入門 (II).  版面配置  事件驅動  Ch14_01.java 1. import javax.swing.*; 2. import java.awt.*; 3. class Ch14_01 4. { 5. public static void main(String.
Event Handling.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 12 Event-Driven Programming 找出画中真谛 — 保罗. 塞尚.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Creating Graphical User Interfaces
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Multidimensional Array Outline Two-Dimensional Arrays Lengths of Two-Dimensional Arrays Multidimension Arrays [Sample code]
Event Handling Events and Listeners Timers and Animation.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 15 Event-Driven Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Event-Driven Programming Event:  A signal to the program that something has happened.  It can be triggered either by external user actions, such as mouse.
Thinking in Objects Outline The this Reference [Sample code] PressEnter.java.
Event-Driven Programming
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.
Outline Overloading Methods The Scope of Variables The Math Class Floating point Format [Sample code] TestMethodOverloading.java.
Applet and Multimedia Outline Developing Applets Applet Life-Cycle Method Passing Strings to Applets [Sample code] DisplayLabel.html.
OBJECTS AND CLASSES Outline Reference Variables and Reference Types Primitive Types and Reference Types Static Variable, Constants,
String and Text I/O Outline The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters,
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 16 Event-Driven Programming.
Java class Outline Defining a method Calling Method Passing parameters [Sample code] TestMethod.java 、 TestMethod2.java 、 GCD.java 、 prime.java.
EVENTS CSC 171 FALL 2004 LECTURE 16. “Traditional” Input In console applications, user input is under control of the program The program asks the user.
Chapter Day 9. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 8 Questions from last Class?? Problem set 2 posted  10 programs from.
1 Event-Driven Programming Just like designing GUIs, you also will probably not write Java code like the program examples given in these notes. You will.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Introduction to Java Programming
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Creating User Interfaces Event-Driven Programming.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
EVENT-DRIVEN PROGRAMMING Subject:T0934 / Multimedia Programming Foundation Session:2 Tahun:2009 Versi:1/0.
1 Chapter 3 Event-Driven Programming. 2 Objectives F To explain the concept of event-driven programming (§12.2). F To understand event, event source,
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Event Handling CS 21a: Introduction to Computing I First Semester,
UQC117S2 Graphics Programming Lecture 2 Event Handling Program as a sequence of instructions Event -driven program Need to detect the event and carry out.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Dept. of CSIE, National University of Tainan 10/21/2012 Responding to User Input.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming using Java - Event Handling
Events and Event Handling
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
Chapter 12 Event-Driven Programming
GUI III IS
Programming in Java Event Handling
Event Handling CS 21a: Introduction to Computing I
GUI Programming III: Events
Event-driven programming for GUI
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Chapter 16 Event-Driven Programming
Inner Classes.
Presentation transcript:

Event-Driven Programming

Outline Event and Event Source Event Types Listeners,Registerations,and Handling Events Event’s Declaration How to declar the Listener Key Events [Sample code] ControlCircle1.java ControlCircle2.java ButtonExample.java KeyEventDemo.java AnimationDemo.java

Event and Event Source The hierachical relationships of some event classes.

Event Types User ActionSource ObjectEvent Type Fired Click a buttonJButtonActionEvent Press return on a text fieldJTextFieldActionEvent Select a new itemJComboBoxItemEvent, ActionEvent Select item(s)JListListSelectionEvent Click a check buttonJCheckBoxItemEvent, ActionEvent Click a radio buttonJRadioButtonItemEvent, ActionEvent Select a menu itemJMenuItemActionEvent ( 課本 P.559)

Listeners,Registerations,and Handling Events Event ClassListener InterfaceListener Methods MouseEventMouseListenermousePressed(MouseEvent) mouseReleased(MouseEvent) mouseEntered(MouseEvent) mouseExited(MouseEvent) mouseClick(MouseEvent) MouseMotionListenermouseDrogged(MouseEvent) mouseMoved(MouseEvent) ( 課本 P.561)

Event’s Declaration EX: JButton jbtOK = new JButton(“OK”); ActionListener listener1 = new OKListenerClass(); jbtOK.addActionListener(listener1); ( When you click the button, the JButton object fires an ActionEvent and presses it to invoke the listener’s actionPerformed method to handle the event )

程式範例 :  ControlCircle1.java ( 課本 P.563)

How to declar the Listener 1.Define a listener class named EnlargeListener that implements ActionListener. 2.Create a listener and register it with jbtEnlarge. 3.Add a method named enlarge() in CirclePanel to increase the radius, then repaint the panel. 4.Implement the actionPerformed method in EnlargeListener to invoke cavas.enlarge(). 5.To make the reference variable canvas accessible from the actionPerformed method, define EnlargeListener as an inner class of the ControlCircle2 class.

How to declar the Listener 1. 宣告 Listener class, implement ActionListener, 並且 編輯 actionPerformed. 2. 用 Button.addActionListener 註冊此 Listener 事件. 3. 在 Panel 裡定義事件發生的 action.

程式範例 :  ControlCircle2.java  ButtonExample.java ( 課本 P.563)

Key Events ( 課本 P.579) ConstantDescriptionConstantDescription VK_HOMEThe Home keyVK_SHIFTThe Shift key VK_ENDThe End keyVK_BACK_SPACEThe Backspace key VK_PGUPThe Page Up keyVK_CPAS_LOCKThe Caps Lock key VK_PGDNThe Page Down key VK_NUM_LOCKThe Num Lock key VK_UPThe up-arrow keyVK_ENTERThe Enter key VK_DOWNThe down-arrow key VK_UNDEFINEDThe keyCode unknow VK_LEFTThe left-arrow key VK_F1 to VK_F12The function keys from F1 to F12

程式範例 :  KeyEventDemo.java

Timer Class Javax.swing.Timer +Timer(delay: int, listener: ActionListener) +addActionListener(listener: ActionListener): void +start(): void +stop(): void +setDelay(delay: int): void

程式範例 :  AnimationDemo.java

程式練習 :  DrawArcs.java 加入 Timer, 修改成會旋轉的扇型