AWT的語義事件和低級事件

  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監聽器接口、事件和事件源。

表1 事件處理總結
接口方法參數/訪問方法事件源
ActionListeneractionPerformedActionEvent
*getActionCommand
*getModifiers
AbstractButton
JComboBox
JTextField
Timer
AdjustmentListeneradjustmentValueChangedAdjustmentEvent
*getAdjustable
*getAdjustmentType
*getValue
JScrollbar
ItemListeneritemStateChangedItemEvent
*getItem
*getItemSelectable
*getStateChange
AbstractButton
JComboBox
FocusListenerfocusGained
focusLost
FocusEvent
*isTemporary
Component
KeyListenerkeyPressed
keyReleased
keyTyped
KeyEvent
*getKeyChar
*getKeyCode
*getKeyModifiersText
*getKeyText
*isActionKey
Component
MouseListenermousePressed
mouseReleased
mouseEntered
mouseExited
mouseClicked
MouseEvent
*getClickCount
*getX
*getY
*getPoint
*translatePoint
Component
MouseMotionListenermouseDragged
mouseMoved
MouseEventComponent
MouseWheelListenermouseWheelMovedMouseWheelEvent
*getWheelRotation
*getScrollAmount
Component
WindowListenerwindowClosing
windowOpened
windowIconified
windowDeiconified
windowClosed
windowActivated
windowDeactivated
WindowEvent
*getWindow
Window
WindowFocusListenerwindowGainedFocus
windowLostFocus
WindowEvent
*getOppositeWindow
Window
WidnowStateListenerwindowStateChangedWindowEvent
*getOldState
*getNewState
Widnow

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章