Interesting understanding of Java event Processing logic

Source: Internet
Author: User
Tags event listener

Interesting understanding of Java event Processing logic

Target : When there is something to walk through door, it is necessary to publish this message on wall.

Analysis : In order to reach the target, it is obvious that an object (event listener) is needed to monitor the door, and the event listener responds when the door has a different action.

(1) We ask a dog to be the listener of the door event.
We are on the door side, adding an event listener, dog, to express this meaning in the Java language:
Door.addactionlistener (New Dog ());

(2) It is obvious that an ordinary dog is not competent to work as a janitor, and must have the gatekeeper skills to be implements ActionListener.
So we're going to define the dog class with this trait, which is expressed in the Java language:
Class Dog Implements ActionListener
{...}

(3) The most important behavior and method of the watchdog is that when the door has an event, the ActionEvent E event object is transmitted, automatically taking action, that is actionperformed (ActionEvent e),
What action does it take? It is:
public void actionperformed (ActionEvent e)
{...}
Haha, it depends on how you give three points.

The reference program is as follows :

Import Java.applet. ;
Import java.awt.
;
Import java.awt.event.*;

public class Yan22 extends Applet
{
TextField Door,wall;
public void Init ()
{
Door=new TextField (10);
Wall=new TextField (20);
Add (door);
Add (wall);
Door.addactionlistener (New Dog ()); Let the dog object monitor Text1
}

Class Dog Implements ActionListener
{
public void actionperformed (ActionEvent e)
{
Wall.settext ("Door has input! ");
}
}
}

Interesting understanding of Java event Processing logic

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.