Top Banner
Java@Ch16. Event-Driven Programming 2011.04.08
15

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

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java @Ch16. Event-Driven Programming 2011.04.08. Outline Event and Event Source Event Types Listeners,Registerations,and Handling Events Event’s Declaration.

Java@Ch16. Event-Driven Programming

2011.04.08

Page 2: Java @Ch16. Event-Driven Programming 2011.04.08. Outline Event and Event Source Event Types Listeners,Registerations,and Handling Events Event’s Declaration.

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

Page 3: Java @Ch16. Event-Driven Programming 2011.04.08. Outline Event and Event Source Event Types Listeners,Registerations,and Handling Events Event’s Declaration.

Event and Event Source

The hierachical relationships of some event classes.

Page 4: Java @Ch16. Event-Driven Programming 2011.04.08. Outline Event and Event Source Event Types Listeners,Registerations,and Handling Events Event’s Declaration.

Event Types

User Action Source Object Event Type Fired

Click a button JButton ActionEvent

Press return on a text field JTextField ActionEvent

Select a new item JComboBox ItemEvent, ActionEvent

Select item(s) JList ListSelectionEvent

Click a check button JCheckBox ItemEvent, ActionEvent

Click a radio button JRadioButton ItemEvent, ActionEvent

Select a menu item JMenuItem ActionEvent

( 課本 P.559)

Page 5: Java @Ch16. Event-Driven Programming 2011.04.08. Outline Event and Event Source Event Types Listeners,Registerations,and Handling Events Event’s Declaration.

Listeners,Registerations,and Handling Events

Event Class Listener Interface Listener Methods

MouseEvent MouseListener mousePressed(MouseEvent)

mouseReleased(MouseEvent)

mouseEntered(MouseEvent)

mouseExited(MouseEvent)

mouseClick(MouseEvent)

MouseMotionListener mouseDrogged(MouseEvent)

mouseMoved(MouseEvent)

( 課本 P.561)

Page 6: Java @Ch16. Event-Driven Programming 2011.04.08. Outline Event and Event Source Event Types Listeners,Registerations,and Handling Events Event’s Declaration.

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 )

Page 7: Java @Ch16. Event-Driven Programming 2011.04.08. Outline Event and Event Source Event Types Listeners,Registerations,and Handling Events Event’s Declaration.

程式範例 :

ControlCircle1.java

( 課本 P.563)

Page 8: Java @Ch16. Event-Driven Programming 2011.04.08. Outline Event and Event Source Event Types Listeners,Registerations,and Handling Events Event’s Declaration.

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.

Page 9: Java @Ch16. Event-Driven Programming 2011.04.08. Outline Event and Event Source Event Types Listeners,Registerations,and Handling Events Event’s Declaration.

How to declar the Listener

1. 宣告 Listener class, implement ActionListener , 並且編輯 actionPerformed.

2. 用 Button .addActionListener 註冊此 Listener 事件 .

3. 在 Panel 裡定義事件發生的 action.

Page 10: Java @Ch16. Event-Driven Programming 2011.04.08. Outline Event and Event Source Event Types Listeners,Registerations,and Handling Events Event’s Declaration.

程式範例 :

ControlCircle2.java

ButtonExample.java

( 課本 P.563)

Page 11: Java @Ch16. Event-Driven Programming 2011.04.08. Outline Event and Event Source Event Types Listeners,Registerations,and Handling Events Event’s Declaration.

Key Events

( 課本 P.579)

Constant Description Constant Description

VK_HOME The Home key VK_SHIFT The Shift key

VK_END The End key VK_BACK_SPACE The Backspace key

VK_PGUP The Page Up key VK_CPAS_LOCK The Caps Lock key

VK_PGDN The Page Down key

VK_NUM_LOCK The Num Lock key

VK_UP The up-arrow key VK_ENTER The Enter key

VK_DOWN The down-arrow key

VK_UNDEFINED The keyCode unknow

VK_LEFT The left-arrow key

VK_F1 to VK_F12 The function keys from F1 to F12

Page 12: Java @Ch16. Event-Driven Programming 2011.04.08. Outline Event and Event Source Event Types Listeners,Registerations,and Handling Events Event’s Declaration.

程式範例 :

KeyEventDemo.java

Page 13: Java @Ch16. Event-Driven Programming 2011.04.08. Outline Event and Event Source Event Types Listeners,Registerations,and Handling Events Event’s Declaration.

Timer Class

Javax.swing.Timer

+Timer(delay: int, listener: ActionListener)

+addActionListener(listener: ActionListener): void

+start(): void

+stop(): void

+setDelay(delay: int): void

Page 14: Java @Ch16. Event-Driven Programming 2011.04.08. Outline Event and Event Source Event Types Listeners,Registerations,and Handling Events Event’s Declaration.

程式範例 :

AnimationDemo.java

Page 15: Java @Ch16. Event-Driven Programming 2011.04.08. Outline Event and Event Source Event Types Listeners,Registerations,and Handling Events Event’s Declaration.

程式練習 :

DrawArcs.java

加入 Timer, 修改成會旋轉的扇型