[Core Java Notes] Beginner event handling

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.