Java Event Listening Mechanism learning

Source: Internet
Author: User

//Event Monitoring Mechanism   Importjava.awt.*; Importjava.awt.event.*;  Public classTestEvent { Public Static voidMain (string[] args) {Frame F=NewFrame ("Test"); Button b=NewButton ("Press me!"); Monitor BH=NewMonitor ();//an object of a class that implements a listener interfaceB.addactionlistener (BH); //RegisterF.add (b, Borderlayout.center);          F.pack (); F.setvisible (true); }  }    //A class that implements some kind of listener interfaceclassMonitorImplementsActionListener { Public voidactionperformed (ActionEvent e) {System.out.println ("A button has been pressed"); }} [Java]Importjava.awt.*; Importjava.awt.event.*;  Public classtestactionevent { Public Static voidMain (string[] args) {Frame F=NewFrame ("Test"); Button B1=NewButton ("Start"); Button B2=NewButton ("Stop"); //a listener class is monitored by two buttonsMonitor2 BH =NewMonitor2 ();          B1.addactionlistener (BH);            B2.addactionlistener (BH); B2.setactioncommand ("Game over"); F.add (B1,"North"); F.add (B2,"Center");            F.pack (); F.setvisible (true); }    }    classMonitor2ImplementsActionListener { Public voidactionperformed (ActionEvent e) {System.out.println ("A button has been pressed," + "the relative info is:\n" +E.getactioncommand ()); }  }  

Quoted http://www.2cto.com/kf/201205/133664.html

http://ericliu1986.iteye.com/blog/629562

The Java event mechanism consists of three parts: events, event listeners, and event sources.

1, events. Typically inherits from the Java.util.EventObject class, encapsulating the event source object and the information associated with the event.

Com.javaedu.event.CusEvent class

 Packagecom.javaedu.event;ImportJava.util.EventObject;/*** Event class for encapsulating event sources and some event-related parameters. *@authorEric*/ Public classCuseventextendsEventObject {Private Static Final LongSerialversionuid = 1L; PrivateObject source;//Event Source         Publiccusevent (Object source) {Super(source);  This. Source =source; }     PublicObject GetSource () {returnsource; }     Public voidSetSource (Object source) { This. Source =source; }}

2, Event listener. Implement Java.util.EventListener interface, register on the event source, when the event source property or state changes, get the corresponding listener calls its internal callback method.

Com.javaedu.event.CusEventListener class

 Packagecom.javaedu.event;ImportJava.util.EventListener;/*** Event listener to implement Java.util.EventListener interface. Define the callback method, put what you want to do * in this method, because the event source occurs when the corresponding event will be called this method. * @authorEric*/ Public classCuseventlistenerImplementsEventListener {//callback method After an event occurs     Public voidfirecusevent (cusevent e) {eventsourceobjecteobject=(Eventsourceobject) E.getsource (); System.out.println ("My name has been changed!");        System.out.println ("I got a new name,named \" "+eobject.getname () +" \ ""); }}

3. Source of the event. Where an event occurs, an event occurs because a property or state of the event source has changed (such as Button clicked, the value of the textbox has changed, and so on). In other words, the corresponding event object is generated. Because event listeners are registered on the event source, the event source class should have a container for the listener (List,set, and so on).

Com.javaedu.event.EventSourceObject class

 Packagecom.javaedu.event;ImportJava.util.HashSet;ImportJava.util.Iterator;ImportJava.util.Set;/*** Event source. *@authorEric*/ Public classEventsourceobject {PrivateString name; //Listener Container    PrivateSet<cuseventlistener>Listener;  PublicEventsourceobject () { This. Listener =NewHashset<cuseventlistener>();  This. Name = "DefaultName"; }    //registering listeners for event sources     Public voidAddcuslistener (Cuseventlistener cel) { This. Listener.add (CEL); }    //notifies all listeners registering on the event source to respond appropriately when an event occurs (call callback method)    protected voidnotifies () {Cuseventlistener cel=NULL; Iterator<CusEventListener> iterator = This. Listener.iterator ();  while(Iterator.hasnext ()) {cel=Iterator.next (); Cel.firecusevent (NewCusevent ( This)); }    }     PublicString GetName () {returnname; }    //simulates an event trigger that triggers an event when the value of the member variable name changes.      Public voidsetName (String name) {if(! This. Name.equals (name)) {             This. Name =name;        Notifies (); }          }}

The following is the Main method class

Com.javaedu.event.MainTest class

 Packagecom.javaedu.event; Public classMaintest {/**     * @paramargs*/     Public Static voidMain (string[] args) {Eventsourceobject object=NewEventsourceobject (); //Register ListenerObject.addcuslistener (NewCuseventlistener () {@Override Public voidfirecusevent (cusevent e) {Super. Firecusevent (e);        }        }); //Triggering EventsObject.setname ("Eric"); }}

Java event listening mechanism learning

Related Article

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.