SWTJFACE event model

xiaoxiao2021-03-06  19

7.1 Four Writings for Events

The SWT event model is the basics of the Java standard AWT. In the example of Chapter 6, how to implement the event response of the text box? For example: the mouse double-click the text box to pop up a dialog. It will be implemented in four ways of events.

7.1.1 Anonymous internal class

Insert the following statement in the original code line "text = new text (shell, swt.border);" Insert the following statement:

// AddMouseListener listener joining mouse event

Text.addmouselistener (new mouseadapter () {

Public void mouseDoubleClick (mouseevent e) {// Mogue Double-click Event Method

/ / Open a message box

MessageDialog.openInformation (NULL, "", "Hello World");

}

});

New MouseAdapter () is an anonymous internal class. We have established a class inherited in mouseadapter, but did not name this class and did not use the usual way of writing, but directly wrote the class code directly in the text.addmofistener method. This is the so-called anonymous internal class (more See Java underlying books for a detailed explanation.

Use an anonymous internal class to write event code simple and convenient, but also pay attention to some of its shortcomings:

l Since the event processing code will be dispersed with the components in the parties in the code, it will cause the code to read and maintain the inconvenience.

l The processing of each event is composed of nested blocks, and some chaos will be visually displayed. If the event processing code is very long, it will also cause inconvenience on reading and maintenance.

l When the toolbar, the menu section, etc. also needs to handle the same user behavior, and the processing code in the event cannot be reused, resulting in bloated code.

7.1.2 Naming internal class

Event code uses a named internal class to solve the problem of anonymous internal classes: First, the event processing code is set together, and all have meaningful names, and the program is easy to read and maintain; in addition, a single event handler is also Can be reused by the toolbar, menu bar. The implementation code is as follows:

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");

}

}

}

7.1.3 External Category Writing

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. This kind of writing is the same as the name of the internal class, but because it is written as a file separately, it will be troublesome. 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");

}

}

7.1.4 Realizing the List of Listening Interfaces

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");

}

}

7.1.5 Summary

Anonymous internal classes are written, but it is not suitable for the event code too much. From the perspective of code writing, reading, maintenance, and program scalability, naming internal classes are most recommended. The external class is mainly used to use the code reuse. If the class outside the package is used to use this event processing code, the external class is sent to the field. And the fourth way of writing, requires that the components can be used when the event code can be shared.

7.2 General Event Introduction

In addition to addMouseListener for responding to the mouse event, Eclipse has some common listeners, which are the same in each component (if the component supports such an event). Here will be briefly described below:

l AddSelectionListener: This listener is most common.

a) WidgetSelected method: The event handler of this method is triggered when the component is selected (the mouse click, press the Enter key).

b) WidgetDefaultSelected method: For some components that are rarely triggered, this method is also very useful in actual development. For example, the text box carries the event, the list box double-click event, etc., you can only use the widgetdefaultselected method to invalidate with the widgetSelected method. l AddkeyListener (button)

a) Keypressed method: When the current focus stops, press the keyboard to call any button. However, this method cannot be performed for some components (such as button button).

b) KeyreleaseD method: Trigger when the key is bomb.

l addfocuslistener (focus)

a) Focusgained method: Trigger when focusing.

b) Focuslost method: trigger when the focus is lost

l Addmouseristener (mouse)

a) MOUSEDOWN method: Trigger when the mouse is pressed

b) MouseUp method: Trigger when the mouse is released

c) MOUSEDOUBLECLICK method: Trigger when the mouse doubles

The above is a commonly used incident, very few, SWT's event model is very easy to master. In fact, except for AddSelectionListener is more commonly used, others are basically very rarely used.

7.3 How to Access the variables in the class in the event code

7.3.1 Three ways to access variables in the class

When writing an event code, you often need to reference variables in the primary class. To access these variables, you need some techniques.

Method 1: Add Final Modifier.

Public class helloworld {

Public static void main (String [] args) {

......

/ / Plus the variable before adding the variable, otherwise it cannot be referenced in the event code

Final String str = "Chen Gang";

Text.addmouselistener (new mouseadapter () {

Public void mousedoubleclick (mouseevent e) {

System.out.println (STR); // STR variable to add Final

}

});

......

}

}

Method 2: Turn the variable STR into an instance variable of the class. However, this method expands the effective range of the STR variable.

Public class helloworld {

// Since the code is referenced to add static in a static method, it will not be necessary.

STATIC STRING STR = "Chen Gang";

Public static void main (String [] args) {

......

}

}

Method 3: Write the event code into a named internal class and then incorporated by constructing the parameters of the function. This method is more cumbersome.

Public class helloworld {

Public static void main (String [] args) {

String str = "Chen Gang";

// Enter the STR value into the STR value via the constructor parameter

Text.Addmouselistener (New MyMouseDoucast (STR);

}

// Anonymous internal class MyMOUsedoubleClick

Private static final class mymouseedoubleclick experts mouseadapter {

Private string string; // Built a variable reference to the value of the STR

Public mymousedouble -ouclick (string str) {// accepts STR value this.String = STR by constructor parameters;

}

Public void mousedoubleclick (mouseevent e) {

System.out.println (String);

}

}

}

7.3.2 Modal and description of variables in Java

This section has been used in some Java variables in this section, attached thereto. There are three easily confused variables in Java: local variables, example variables, class variables, as shown in the following procedures:

Public class variable {

Static int allClicks = 0; // class variable

String str = "Guangxi Guilin"; // instance variable

Public void method () {

INT i = 0; // Local variable

}

}

Before the definition of class variables, add static, indicating that it is a static, so a plurality of instances of the class share a class variable. The instance variable defines the method of the class, typically in the starting position of the class, each instance of the class has a copy of the instance variable alone. The effective range of local variables in blocks, its life period is limited to this block.

Example variables are also translated into "domain" or "member variables" in some books. In the real class (Hibernate, also known as the POJO - Simple Original Java object), the variable called "Properties" or "field" is also one of the instance variables.

The general principle of using variables is to minimize the valid range of variables: prioritize local variables, followed by instance variables, and finally the class variable.

In addition, there is a constant write method, which is only more final than class variables, as shown below:

Final static int all_clicks = 0; // constant

Note that all_clicks is all written, which is a constant specification naming method. At this time, all_clicks is constrained by Final, which cannot be assigned again.

7.4 This chapter is summarized

This chapter mainly introduces four ways of writing, and methods of variables in event access classes, which are often used in SWT programming. However, readers can not have to attract it too much about this chapter, and after quickly browse, go to the next chapter, when used, go back and check.

Author: Chen Gang

转载请注明原文地址:https://www.9cbs.com/read-42500.html

New Post(0)