Why do I need adapter? Short answer: Simplifying
"Event Source" can register (addxxxlistener) Listener object and Send event object
WindowListener listener = ...; Frame.addwindowlistener (listener);
Specific event corresponding to a specific listener interface (Listener interface);
Public Interface windowlistener{ void windowopened (windowevent e); void windowclosing (WindowEvent e);}
In order to handle a particular event, the listener implements the corresponding interface , which in Java means all methods (windowopened, windowclosing) that need to implement the interface . )
But the listener may only be interested in one of these methods, and other methods don't need to do anything, so it can be tedious to implement:
class Implements windowlistener{ publicvoid windowclosing (windowevent e) { if (user agrees) System.exit (0); } Public void windowopened (WindowEvent e) {} ...}
To simplify, each AWT listener interface with multiple methods has an adapter (Adapter) class that implements all the methods in the interface, but each method does nothing. Need to construct monitoring
Terminator_b, you only need to extend (extends) this adapter and overriding the function of interest. :
In practice, when you create a listener class, we define the listener as an anonymous intrinsic to the event source, as follows:
Frame.addwindowlistener (new windowadapter () { publicvoid Windowclosing (WindowEvent e) { if(user agrees) System.exit (0); }});
Listener and Adapter Listener&adapter