------- Android training, Java training, and hope to communicate with you!
----------
In the past, although I tried my best to explain the implementation process when studying AWT event processing, I still didn't understand why the button called the listener method. I just remembered this point. Yesterday, I understood the Internal principles of the observer model that Jack Ma told me. Sometimes it's just like learning java. If you don't know how it is implemented, remember it, and remember it. Once you know how it works, you will understand it, it is easy to use. Therefore, JDK class libraries are often used. If you can, it is better to understand their implementation principles.
The following is the first version of code for simulating AWT events.
Package c21designpattern; import Java. util. arraylist; import Java. util. list;/*** function: simulates the practice processing mechanism of the AWT framework in JDK, and directly calls the button in the test class without simulating the graphic interface. press (), indicating that the event is generated. * Key: The Observer mode is used. The event handler notifies the event handler, rather than the event handler, to listen to the event handler. * @ Author renyajun **/public class AWT {public static void main (string [] ARGs) {// new button, event listener, add event listener, activation event button BTN = new button (); actioneventlistener Al = new button1actionlistener (); BTN. addactioneventlistener (Al); BTN. press () ;}// button in awt to simulate the button. Key points: 1. press method, indicating that the button is pressed, 2. list <ationeventlistener> indicates all the handlers of the pressed Button // event. Class button {list <actioneventlistener> actioneventlisteners = new arraylist <actioneventlistener> (); Public void Press () {for (actioneventlistener L: actioneventlisteners) {L. acionreceivmed () ;}} public void addactioneventlistener (actioneventlistener Al) {actioneventlisteners. add (Al) ;}} interface actioneventlistener {void acionreceivmed ();} class button1actionlistener implements actioneventlistener {@ overridepub LIC void acionreceivmed () {system. Out. println ("the content of the text box changes to XXX. ");}}
The second version adds an event class so that the client can obtain the event occurrence time and time source through the event parameters. The Code is as follows:
Package c21designpattern; import Java. util. arraylist; import Java. util. list;/*** function: simulates the practice processing mechanism of the AWT framework in JDK, and directly calls the button in the test class without simulating the graphic interface. press (), indicating that the event is generated. * Key: The Observer mode is used. The event handler notifies the event handler, rather than the event handler, to listen to the event handler. * @ Author renyajun **/public class AWT {public static void main (string [] ARGs) {// new button, event listener, add event listener, activation event button BTN = new button ("analog button"); actioneventlistener Al = new button1actionlistener (); BTN. addactioneventlistener (Al); BTN. press () ;}// button in awt to simulate the button. Key points: 1. press method, indicating that the button is pressed, 2. list <ationeventlistener> indicates all the handlers of the pressed Button // event. Class button {list <actioneventlistener> actioneventlisteners = new arraylist <actioneventlistener> (); Private string name; Public button (string name) {This. name = Name;} public void Press () {actionevent E = new actionevent (system. currenttimemillis (), this); For (actioneventlistener L: actioneventlisteners) {L. acionreceivmed (e) ;}} public void addactioneventlistener (actioneventlistener Al) {actioneventlisteners. add (Al) ;}@ Overridepublic string tostring () {return name ;}} interface actioneventlistener {void acionreceivmed (actionevent E);} class button1actionlistener implements actioneventlistener {@ overridepublic void acionreceivmed (actionevent E) {system. out. println ("the content of the text box changes to XXX. "); System. out. println ("event occurred at:" + E. gettime () + ", event Source:" + E. getsource () ;}} class actionevent {private long time; private object source; Public actionevent (long time, object source) {This. time = time; this. source = source;} public long gettime () {return time;} public object getsource () {return source ;}}