next up previous
: イベント処理のプログラム例 : イベントの取り扱い : イベント駆動型プログラム

イベントとリスナーのクラス

コンピュータのGUI(Graphical User Interface)は常に新しくなっており,将来,どの ようなイベントの処理がプログラムに要求されるかは分からないため,Appletク ラスには特定のイベントを監視し,その要求を受け取るという機能は備わっていない [*]

その代わりにリスナーと呼ばれるインターフェイスを利用する. リスナーはイベントに対応して,これを受け取るメソッドを持っている. 表 [*]にイベントとそれに対応するイベントクラスおよびリスナー,リス ナーが有するメソッドを列挙する.


表: Java1.1以降におけるイベントとリスナの対応
イベントクラス リスナインターフェイス リ スナメソッド
ActionEvent ActionListener actionPerformed()
AdjustmentEvent AdjustmentListener adjustmentValueChanged()
ComponentEvent ComponentListener componentHidden()
    componentMoved()
    componentResized()
    componentShown()
ContainerEvent ContainerListener componentAdded()
    componentRemoved()
FocusEvent FocusListener focusGained()
    focusLost()
ItemEvent ItemListener itemStateChanged()
KeyEvent KeyListener keyPressed()
    keyReleased()
    keyTyped()
MouseEvent MouseListener mouseClicked()
    mouseEntered()
    mouseExited()
    mousePressed()
    mouseReleased()
  MouseMotionListener mouseDragged()
    mouseMoved()
TextEvent TextListener textValueChanged()
WindowEvent WindowListener windowActivated()
    windowClosed()
    windowClosing()
    windowDeactivated()
    windowDeiconified()
    windowIconified()
    windowOpened()

イベントが発生した際には,イベントクラスのインスタンスがリスナメソッドに引数とし て渡され,これに基づいて必要な情報を得ることができる. 例えば, MouseEventクラスは getX() getY()というメソッドを有して おり,イベントの発生した時点におけるマウスカーソルの位置を調べることができる.


next up previous
: イベント処理のプログラム例 : イベントの取り扱い : イベント駆動型プログラム
U-GM\murao 平成14年7月5日