AWT的语义事件和低级事件

本文深入探讨了Java AWT中的事件类型,包括低级事件和语义事件,重点介绍了常用的语义事件类如ActionEvent、AdjustmentEvent、ItemEvent等,以及与之对应的低级事件类如KeyEvent、MouseEvent等。同时,文章详细介绍了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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值