java.awt.event.* 事件速用表

ActionListener
AdjustmentListener
AWTEventListener
ComponentListener
ContainerListener
FocusListener
HierarchyListener、HierarchyBoundsListener
InputMethodListener
ItemListener
KeyListener
MouseListener、MouseMotionListener、MouseWheelListener
PropertyChangeListener
TextListener
WindowListener、WindowFocusListener、WindowStateListener
移除事件


ActionListener


actionPerformed


event.getModifiers() & ActionEvent.SHIFT_MASK) != 0 判斷是否按下 Shift 鍵。ALT_MASK 和 CTRL_MASK 依此類推。


AdjustmentListener


adjustmentValueChanged


AWTEventListener


eventDispatched


ComponentListener


componentShown


componentHidden


componentMoved


componentResized


ContainerListener


componentAdded


componentRemoved


FocusListener


focusGained


focusLost


HierarchyListener、HierarchyBoundsListener


hierarchyChanged


ancestorMoved


ancestorResized


InputMethodListener


caretPositionChanged


inputMethodTextChanged


ItemListener


itemStateChanged


KeyListener


keyPressed


keyReleased


keyTyped


可根據 event.getKeyCode() 的傳回值,判斷使用者按下哪一個滑鼠鍵:

KeyEvent.VK_A ~ KeyEvent.VK_Z
KeyEvent.VK_0 ~ KeyEvent.VK_9
KeyEvent.VK_NUMPAD0 ~ KeyEvent.VK_NUMPAD9
KeyEvent.VK_F1 ~ KeyEvent.VK_F12

KeyEvent.VK_BACK_QUOTE`
KeyEvent.VK_MINUS-
KeyEvent.VK_EQUALS=
KeyEvent.VK_OPEN_BRACKET[
KeyEvent.VK_CLOSE_BRACKET]
KeyEvent.VK_BACK_SLASH\
KeyEvent.VK_SEMICOLON;
KeyEvent.VK_COMMA,
KeyEvent.VK_PERIOD.
KeyEvent.VK_SLASH/

KeyEvent.VK_ESCAPE
KeyEvent.VK_TAB
KeyEvent.VK_CAPS_LOCK
KeyEvent.VK_SHIFT
KeyEvent.VK_CONTROL
KeyEvent.VK_WINDOWS
KeyEvent.VK_ALT
KeyEvent.VK_SPACE
KeyEvent.VK_BACKSPACE
KeyEvent.VK_ENTER

KeyEvent.VK_PAUSE
KeyEvent.VK_PRINTSCREEN
KeyEvent.VK_INSERT
KeyEvent.VK_DELETE
KeyEvent.VK_HOME
KeyEvent.VK_END
KeyEvent.VK_PAGE_UP
KeyEvent.VK_PAGE_DOWN

KeyEvent.VK_UP
KeyEvent.VK_DOWN
KeyEvent.VK_LEFT
KeyEvent.VK_RIGHT

還有更多傳回值,請參考《Java API Specification》。

根據 event.isAltDown()event.isShiftDown()event.isControlDown() 可判斷是否搭配 Alt、Shift、Ctrl 組合鍵。

綜合範例如下:


MouseListener、MouseMotionListener、MouseWheelListener


mouseClicked


mouseExited


mousePressed


mouseReleased


mouseMoved


mouseDragged


mouseWheelMoved


MouseEvent 可根據 getButton() 的傳回值,判斷使用者按下哪一個滑鼠鍵:

MouseEvent.BUTTON1左鍵
MouseEvent.BUTTON2中鍵
MouseEvent.BUTTON3右鍵

綜合範例如下:


可用 event.isAltDown()event.isShiftDown()event.isControlDown() 判斷是否同時按下 Alt、Shift、Ctrl 組合鍵。

MouseWheelEvent 可根據 getWheelRotation() 傳回值是正數還是負數,可判斷使用者滾輪是往上滾動還是往下:


PropertyChangeListener


propertyChange


TextListener


textValueChanged


WindowListener、WindowFocusListener、WindowStateListener


windowActivated


windowClosed


windowClosing


windowDeactivated


windowDeiconified


windowIconified


windowOpened


windowGainedFocus, windowLostFocus


沒有 WindowsFocusAdapter。


windowStateChanged


移除事件

可透過以下方式,取消登記過的事件:

removeComponentListener(ComponentListener)
removeFocusListener(FocusListener)
removeHierarchyBoundsListener(HierarchyBoundsListener)
removeHierarchyListener(HierarchyListener)
removeInputMethodListener(InputMethodListener)
removeKeyListener(KeyListener)
removeMouseListener(MouseListener)
removeMouseMotionListener(MouseMotionListener)
removeMouseWheelListener(MouseWheelListener)
removePropertyChangeListener(PropertyChangeListener)