java @ch16. event-driven programming 2011.04.08. outline event and event source event types...

15
Java@Ch16. Event-Driven Programming 2011.04.08

Post on 21-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

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• 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 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)

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)

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)

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

程式範例 :

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, 修改成會旋轉的扇型