VB event handling: Each GUI component responds to a fixed set of events and cannot change its set of events.
Event handling in C: Write code with poor readability and unlimited response events.
Java Event Handling: (The tradeoff between VB and C) Event delegation model.
Java encapsulates information about events in event objects . (e.g. button, buttons, etc.)
The ancestor class for all events is java.util.EventObject.
The AWT event handles important participants:
(1) Event Listener : An object that implements a specific listener interface. (Can analyze events)
(2) Event Source : The Listener object is registered, and an event object is sent to him when the event occurs. (Capture the events you manage and pass them on to the listener)
When an event occurs, the event source passes the event object to all registered listeners. (Event source captures events and passes to listeners)
The event listener takes advantage of the information in the event object to determine how to make the event appropriate. (Listener parsing events, responding)
1 ActionListener lsner = ...; 2 3 New JButton ("OK"); 4 5 Btn.addactionlistener (Lsner); // as soon as a button is pressed, Lsner will be notified.
View Code
The listener class that implements the ActionListener interface must have a actionperformed method:
1 classMyListener implements ActionListener2 3 {4 5 ... ..6 7 Public voidActionperformed (ActionEventEvent)8 9{//as soon as the user taps the button, JButton creates a ActionEvent object that calls this method to pass the event object.Ten One //reaction to button click Goes here A - .... - the } - -}View Code
[Core Java Notes] Beginner event handling