The graphical interface software made by Java implements the interaction of the user and the program through the event response mechanism, which is probably like this:
First, add a monitoring object on the Java control object (such as a text box), the method is One.AddxxxListenner (TWO), which is equivalent to listening to someone, first to bind a bug on him. Here "One" is the guy you have to listen, TWO is a bug that you make yourself.
The second step is to consider how to make this eavesdropper. We must first figure out the function it wants: it can not only monitor the one in one, but also tell the system to listen to the event, and let the system do this. There is a corresponding process. Such functions are achieved in Java. For these interfaces, see the java.awt.event package in JDK, the few xxxlistener (not a lot, less common). Some methods are defined in these interfaces that these methods are the corresponding event processing, they are called only by the system and have an object of an event class as a parameter, which is a link that links the event body one and the operating system. Of course, the interface is virtual, can't generate objects; so I want you to guess that the above "Buffet" TWO type is definitely a class that implements the XXXListener interface.
Ok, now review this process:
(1) The user passes the mouse,
Keyboard, etc., do action on one object (such as clicking the mouse),
(2) This action is listened to and generates an event object E (ie XXXEvent object),
(3) TWO reports to the system through the event E object,
(4) System calling the method implemented in TWO Processing events and transmits event E to the method.
If you know this process, let's take a look at this XXXListener interface. We must use classes to implement the interface method (this is the Java basic knowledge ◎), and use this class to generate TWO objects. Class implementation interface must implement each method in the interface, and in fact what we need is just a method in the interface (such as the one to right click on the right click), then each method has to implement an empty, really waste. In order to make programmers saved something, SUN has implemented these interfaces in JDK. These classes are our so-called "Adapter" (XXXADAPTER), we only need to inherit these classes and rewritten inside we need The method is OK, so, in fact, the adapter is class, but only some empty methods are only in these classes.
At this point, you probably understand which things should be done during the event handling process:
(1) Manufacturing "Heave", ie: realize event monitor interface, rewriting related methods,
(2) Install the eavesdropper, namely: add a listener to the monitoring object.
Below, we take a closer look at the implementation of the event response (to be continued):
(The following is referred to Chen Gang "Eclipse from the entrance to")
(
1), anonymous internal class
Routine:
Text.addmouselistener (new mouseadapter () {
Public void mousedoubleClick (MouseEvent E) {// Mouse Double-click Event Method // Open a message
MessageDialog.openInformation (NULL, "", "Hello World");
}
});
New MouseAdapter () is an anonymous internal class. In fact, it is to construct a class of objects in realization, and use it as the parameters of AddMouseListener, the same as the following, only the code is concentrated.
(2
), Naming internal classes
Public class helloworld {
Public static void main (String [] args) {
......
Text text = new text (shell, swt.border);
// Add a mouse event listener and generate an object with the internal class defined by the following code.
Text.addmouselistener (new mymousedoubleclick ());
......
}
/ / Define an internal class called MyMouseDoubleClick
Private static final class mymouseedoubleclick experts mouseadapter {
Public void mousedoubleclick (mouseevent e) {
MessageDialog.openInformation (NULL, "", "Hello World");
}
}
}
(3
), External class
This kind of writing and naming internal classes are somewhat similar, but the MyMouseDoubleClick class will take out from HelloWorld.java and write it into a class file. Implement code as follows
// File 1: HelloWorld.java
Public class helloworld {
Public static void main (String [] args) {
......
Text text = new text (shell, swt.border);
// Add a mouse event listener and generate an object with the internal class defined by the following code.
Text.addmouselistener (new mymousedoubleclick ());
......
}
}
// File 2: MyMouseDoubleClick.java
Public class mymouseadapter {Xtends mouseadapter {
Public void mousedoubleclick (mouseevent e) {
MessageDialog.openInformation (NULL, "", "Hello World");
}
}
(4
), Realize the writing of the monitor interface
The HelloWorld class implements the MouseListener interface so that the class itself has a listener that enables the code to join the listener to be more concise. This approach is suitable for adding more components of the listener, and the event processing code that requires the listener can be shared by the component. This approach has a place to pay attention to: Event methods and other methods are mixed together, it is easy to cause misread, so detailed annotation should be added before the event method.
There are more event methods to be written to implement the mouselistener interface, and of course, the event method that is useless can be empty. If you inherit the MouseListener interface, you only write the method you need. Also pay attention to: Only the interface can have more inheritance, so if helloworld is already a subclass of a class, you can only use the implementation of the interface, and cannot inherit the adapter of the interface. The sample code is given:
Public class helloworld extends mouseadapter {// or imports mouselistener
Public static void main (String [] args) {
......
Text text1 = new text (shell, swt.border);
Text text2 = new text (shell, swt.border);
Text1.addmouselistener (this);
Text2.addmouselistener (this);
......
}
Public void mousedoubleclick (mouseevent e) {
MessageDialog.openInformation (NULL, "", "Hello World");
}
}