SlideServe
Menu
Préférences
© SlideServe · v2026

Event Handling

joben avatar
11 diapositives · 305 Views · Aug 01, 2014

Event Handling. Event Handling. User actions are called “events” Low-level window events Window changes Low-level component events Mouse events, Keyboard events, Component changes High-level component-specific events Button events, Menu events, List events, etc.

Event Handling - slide 1
01 / 11Event Handling
1 / 17

Télécharger la présentation

Event Handling
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Event Handling

  2. Event Handling • User actions are called “events” • Low-level window events • Window changes • Low-level component events • Mouse events, Keyboard events, Component changes • High-level component-specific events • Button events, Menu events, List events, etc. • Events are handled by attaching “event listeners” to windows and components • Swing defines a “listener interface” for each category of events • Examples: WindowListener, MouseListener, MenuListener

  3. Event Handling • A “listener” is an object that implements a listener interface • Listeners are attached to windows and components by calling the appropriate “addXXXListener” methods • addWindowListener, addMouseListener, addMenuListener, etc. • The window or component will notify all attached listeners about all relevant events • windowActivated, mouseClicked, menuSelected, etc. • Listener methods accept an EventObject parameter that describes the event that occurred • WindowEvent, MouseEvent, MenuEvent, etc. • The EventObject.getSourcemethod returns the window or component that generated the event

  4. Event Handling • Listeners are typically implemented as inner classes • Named inner class • class MyMouseHandler implements MouseListener { … } • this.addMouseListener(new MyMouseHandler()); • Anonymous inner class • this.addMouseListener(new MouseListener() { … });

  5. Event Handling • Event Adapter classes • Frequently, a program only wants to handle a few of the events defined by a listener interface • However, implementing the listener interface requires implementations for all event methods • Swing provides Adapter superclasses that provide empty implementations of all methods in a listener interface • WindowAdapter, MouseAdapter, KeyAdapter, etc. • This allows a program to create a listener by subclassing an adapter class, and overriding only the event methods it cares about • Some adapter classes implement multiple listener interfaces • EXAMPLE: MouseAdapter implements MouseListener, MouseMotionListener, and MouseWheelListener

  6. Mouse Events • MouseListener interface • MouseMotionListener interface • MouseWheelListener interface • MouseAdapter class • EXAMPLE: Dragging and scrolling shapes in Drawing

  7. Component Events • ComponentListener interface • ComponentAdapter class • EXAMPLE: Updating width and height in text shapes in Drawing

  8. Window Events • WindowListener interface • WindowStateListener interface • WindowAdapter class • EXAMPLE: Frame window in Drawing

  9. Keyboard Events • Keyboard Focus • When the user types a key, which component gets the key event? • The component with the “keyboard focus” • Only one window has the keyboard focus at a time • Within that window, only one component has the keyboard focus at a time • A component can request the keyboard focus by calling Component.requestFocus or Component.requestFocusInWindow • EXAMPLE: A text field requests the keyboard focus when the user clicks on it so it will receive key events

  10. Keyboard Events • When a window receives or loses the keyboard focus, it generates “gained focus” and “lost focus” events • WindowFocusListener interface • When a component receives or loses the keyboard focus, it generates “gained focus” and “lost focus” events • FocusListener interface • EXAMPLE: In Drawing, when the frame window receives keyboard focus, DrawingFrame calls requestFocusInWindow on the DrawingComponent so it will receive keyboard events

  11. Keyboard Events • KeyListener interface • KeyAdapter class • EXAMPLE: Moving shapes with arrow keys in Drawing