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