AWT將事件分爲低級(low-level)事件和語義(semantic)事件。語義事件是表示用戶動作的事件,例如,點擊按鈕;因此,ActionEvent是一種語義事件。低級事件是形成那些事件的事件。在點擊按鈕時,包含了按下鼠標、連續移動鼠標、擡起鼠標(只有鼠標在按鈕區中擡起才引發)事件。或者在用戶利用TAB鍵選擇按鈕,並利用空格鍵激活它時,發生的敲擊鍵盤事件。同樣,調節滾動條是一種語義事件,但拖動鼠標是低級事件。
下面是java.awt.event包中最常用的語義事件類:
× ActionEvent(對應按鈕點擊、菜單選擇、選擇列表項或在文本框中ENTER);
× AdjustmentEvent(用戶調節滾動條);
× ItemEvent(用戶從複選框或列表框中選擇一項)。
常用的5個低級事件類是:
× KeyEvent(一個鍵被按下或釋放);
× MouseEvent(鼠標鍵被按下、釋放、移動或拖動);
× MouseWheelEvent(鼠標滾輪被轉動);
× FocusEvent(某個組件獲得焦點或失去焦點);
× WindowEvent(窗口狀態被改變)。
下列接口將監聽這些事件。
ActionListener,AdjustmentListener,FocusListener,ItemListener,KeyListener,
MouseListener,MouseMotionListener,MouseWheelListener,WindowListener,WindowFocusListener,WindowStateListener
有幾個AWT監聽器接口包含多個方法,它們都配有一個適配器類,在這個類中實現了相應接口中的所有方法,但每個方法沒有做任何事情。(有些接口只包含一個方法,因此,就沒有必要爲它們定義適配器了)下面是常用的適配器類:
FocusAdapter,KeyAdapter,MouseAdapter,MouseMostionAdapter,WindowAdapter
表1顯示了最重要的AWT監聽器接口、事件和事件源。
接口 | 方法 | 參數/訪問方法 | 事件源 |
---|---|---|---|
ActionListener | actionPerformed | ActionEvent *getActionCommand *getModifiers | AbstractButton JComboBox JTextField Timer |
AdjustmentListener | adjustmentValueChanged | AdjustmentEvent *getAdjustable *getAdjustmentType *getValue | JScrollbar |
ItemListener | itemStateChanged | ItemEvent *getItem *getItemSelectable *getStateChange | AbstractButton JComboBox |
FocusListener | focusGained focusLost | FocusEvent *isTemporary | Component |
KeyListener | keyPressed keyReleased keyTyped | KeyEvent *getKeyChar *getKeyCode *getKeyModifiersText *getKeyText *isActionKey | Component |
MouseListener | mousePressed mouseReleased mouseEntered mouseExited mouseClicked | MouseEvent *getClickCount *getX *getY *getPoint *translatePoint | Component |
MouseMotionListener | mouseDragged mouseMoved | MouseEvent | Component |
MouseWheelListener | mouseWheelMoved | MouseWheelEvent *getWheelRotation *getScrollAmount | Component |
WindowListener | windowClosing windowOpened windowIconified windowDeiconified windowClosed windowActivated windowDeactivated | WindowEvent *getWindow | Window |
WindowFocusListener | windowGainedFocus windowLostFocus | WindowEvent *getOppositeWindow | Window |
WidnowStateListener | windowStateChanged | WindowEvent *getOldState *getNewState | Widnow |