Download presentation
Presentation is loading. Please wait.
1
Java @Ch16. Event-Driven Programming 2011.04.08
2
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
3
Event and Event Source The hierachical relationships of some event classes.
4
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)
5
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)
6
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 )
7
程式範例 : ControlCircle1.java ( 課本 P.563)
8
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.
9
How to declar the Listener 1. 宣告 Listener class, implement ActionListener, 並且 編輯 actionPerformed. 2. 用 Button.addActionListener 註冊此 Listener 事件. 3. 在 Panel 裡定義事件發生的 action.
10
程式範例 : ControlCircle2.java ButtonExample.java ( 課本 P.563)
11
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
12
程式範例 : KeyEventDemo.java
13
Timer Class Javax.swing.Timer +Timer(delay: int, listener: ActionListener) +addActionListener(listener: ActionListener): void +start(): void +stop(): void +setDelay(delay: int): void
14
程式範例 : AnimationDemo.java
15
程式練習 : DrawArcs.java 加入 Timer, 修改成會旋轉的扇型
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.