event mechanism in java

The graphical interface software made by Java realizes the interaction between the user and the program through the event response mechanism. The principle is probably as follows:
First, add a monitoring object to the java control object (such as a text box), the method is one.addXXXListenner(two) , which is equivalent to if you want to monitor someone, you must first bind a bug to him, Here "one" is the guy you want to monitor, and two is a bug you made yourself.
The second step is to consider how to build this eavesdropper. We must first figure out the function it wants to achieve: it can not only monitor one's every move, but also tell the system about the events it listens to, and let the system do something about this event. deal with it accordingly. In Java, such functions are implemented through interfaces. For these interfaces, please refer to the java.awt.event package in jdk, and the XXXListeners in it are (not many, less commonly used). Some methods are defined in these interfaces. These methods are the corresponding event processing methods. They are only called by the system and have an event class object as a parameter. This parameter is the link between the event subject one and the operating system. Of course, the interface is virtual and cannot generate objects; so you must have guessed that the type of the "bugger" two above must be a class that implements the XXXListener interface.
Well, now to review the process:
(1) The user performs actions (such as clicking the mouse) on the one object through the mouse, keyboard, etc.,
(2) This action is monitored by two and generates an event object e (that is, the object of XXXEvent),
(3) Two reports to the system through the event e object,
(4) The system calls the method implemented in two to process the event, and transmits the event e to the method.
If you understand this process, take a closer look at the XXXListener interface. We must use classes to implement the methods of the interface (this is the basic knowledge of java ◎), and then use this class to generate objects such as two. A class that implements an interface must implement each method in the interface, but in fact we may only need a method in the interface (such as the one that handles the right mouse button), then each method has to implement an empty one, really waste. In order to save programmers some trouble, Sun has implemented these interfaces for us in JDK. These classes are what we call "adapters" (XXXAdapter), we only need to inherit (extends) these classes and rewrite the inside we need The method is OK, so in fact, the adapter is the class, but there are only some empty methods in these classes, and nothing else.
At this point, you probably understand what the programmer should do in the event processing process:
(1) Create a "bugger", that is: implement the event monitoring interface, rewrite related methods,
(2) Install a bug, that is: add a listener to the monitoring object.
 
Next, let's take a closer look at the implementation of event response through the program (to be continued):
(The following content refers to Chen Gang's "Eclipse from entry to mastery")
( 1 ), anonymous inner class writing
Routine:
text.addMouseListener(new MouseAdapter() {
    public void mouseDoubleClick(MouseEvent e) {//Method of mouse double-click event
         // open a message box
        MessageDialog.openInformation (null,"","Hello World");
    }
});
new MouseAdapter() is an anonymous inner class. In fact, it is to construct an object of this class with new while implementing the class, and use it as the parameter of addMouseListener. Its effect is the same as the following, but the code is more concentrated.
 
(2 ), named inner class writing
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 inner class defined by the following code
        text.addMouseListener(new MyMouseDoubleClick());
        ……
    }
 
//Define an inner class named MyMouseDoubleClick
    private static final class MyMouseDoubleClick extends MouseAdapter {
        public void mouseDoubleClick(MouseEvent e) {
            MessageDialog.openInformation(null, "", "Hello World");
        }
    }
}
 
(3 ), external class writing
This way of writing is somewhat similar to naming inner classes, except that the MyMouseDoubleClick class is taken out of HelloWorld.java and written into a separate class file. The implementation code is 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 inner class defined by the following code
        text.addMouseListener(new MyMouseDoubleClick());
        ……
    }
}
 
//File 2: MyMouseDoubleClick.java
public class MyMouseDoubleClick extends MouseAdapter {
    public void mouseDoubleClick(MouseEvent e) {
        MessageDialog.openInformation(null, "", "Hello World");
    }
}
(4 ), implement the writing method of the listening interface
The HelloWorld class implements the MouseListener interface, so that the class itself becomes a listener, so that the code for adding the listener can be more concise. This method is suitable for many components that add listeners, and requires that the event processing code of the listener can be shared by the components. There is another point to note in this method: the event method is mixed with other methods, which is easy to cause misunderstanding, so detailed comments should be added before the event method.
There are more event methods to write to implement the MouseListener interface. Of course, useless event methods can be implemented empty. If you inherit the adapter MouseAdapter of the MouseListener interface, you only need to write the required methods. Also note: only interfaces can have multiple inheritance features, so if HelloWorld is already a subclass of a class, it can only implement the interface, not the adapter of the interface.
The sample code is given as follows:
public class HelloWorld extends MouseAdapter{//或implements 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");
    }
}
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325849634&siteId=291194637