About SWT

zhaozj2021-02-12  148

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$

Fish and bear's pauses are not good? Up to now, we have introduced two critical technologies for solving the shortcomings of existing J2EE development model: First, SWT, mainly used as the interface performance of the client program; Second, Java Web Start, mainly used as client programs Network release. The next question is how to integrate it into J2EE's architecture to make it a complete solution.

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$

February 2003

Since the birth of Java, it has been successful in too many fields, but it rarely emerges in the graphical interface program. The reason, the default graphical interface development package of Java language is really difficult to get rid of, regardless of speed and appearance, they are difficult to accept. Today, the SWT development kit prepared by Eclipse organizes the Java programmers with a better choice other than AWT and Swing. In this article, SWT is simple but as comprehensive as possible.

Java language prestige and its achievements in desktop applications (GUI programs) are clearly unable to see very successful Java desktop programs. Although there are large software such as JBuilder, NetBean, Jprobe as representatives, but this still cannot prove that the Java's GUI program is successful: their appearance is always incorporated with other software under the same operating system platform. The demand for machine configuration seems to be never end, which can only be tolerated by some of the programmers who always have the current highest performance PC, or those who don't care about money and time. For most computers, AWT or SWING represents a weird interface and the speed of acceptance. Standard Widget Toolkit (SWT) may be the end of Java's nightmare. The majority of Java programmers can finally develop high-efficiency GUI programs that have standard appearance, almost no one can see your program is written in Java. More importantly, these programs are cross-platform.

The SWT itself is only a set of underlying graphical interface APIs written in the Eclipse IDE environment in order to develop the Eclipse IDE environment. Perhaps it is a no heart, or intentionally, so far, SWT has surpassed the AWT and SWING provided by Sun company in performance and appearance. At present, Eclipse IDE has been developed to version 2.1, SWT is already stable. The stability referred to here should include two layers:

First, the performance is stable, and the key is to stem from the design concept of SWT. SWT maximizes the operating system's graphics component API, that is, as long as the operating system provides a component of the corresponding graphic, SWT is simply applied JNI technology to call them, only those that do not provide in the operating system, SWT will be a simulation. Realization. It can be seen that the stability of the performance of the SWT depends on the stability of the corresponding operating system graphics components.

Another stability refers to the names and structures of the SWT API package. The name and structure of the method have changed, and the programmer does not have to worry because the Eclipse organization has developed very fast (Eclipse ID will have a Nightly version release every day), which leads to your own The program code has changed too much. Update from a version of SWT to another, usually only simply replace the SWT package.

The first SWT program lets us start a SWT program. (Note: The following examples and descriptions are primarily for Windows platforms, and other operating systems should be similar. The first thing to find a SWT package in the Eclipse installation file, and the Eclipse organization does not provide a separate SWT package download, you must download the full Eclipse development environment to get the SWT package. SWT is in the form of a plugin as an Eclipse development environment. You can search for a SWT.jar file in a numerous subdirectory in $ {your Eclipse installation path} / plugins, in the found JAR file contains all Java classes in SWT file. Because SWT applies JNI technology, it is also necessary to find a corresponding JNI localized library file. Due to the different version and operation platform, the name of the localized library file will have some differences, such as SWT-Win32-2116.dll is a Window platform. The dynamic library of Eclipse Build 2116, and the extension of the corresponding version of the library file in the UNIX platform should be .so, and so on. Note that Eclipse is an open source project, so you can also find SWT source code in these directories, I believe this is very helpful for development. Below is a code that opens the empty window (only the main method).

Import com.e2one.example;

Public class openshell {

Public static void main (String [] args) {

Display display = new display () and DISPLAY ();

Shell shell = new shell (display);

shell.open ();

// Start the event processing loop until the user closes the window

While (! shell.isdisposed ()) {

IF (! display.readDispatch ())

Display.sleep ();

}

Display.dispose ();

}

}

Confident in the ClassPath included SWT.jar files, use the Javac to compile examples. After compilation, you can run java -djava.library.path = $ {{{你 你}} }. 文件 文件} com.e2one.example.openshell, such as the path where the SWT-Win32-2116.dll is C: / SWTLIB The running command should be java -djava.library.path = c: / swtlib com.e2one.example.Openshell. After successful operation, the system opens an empty window.

Analysis of the SWT API and let us further analyze the composition of the SWT API. All SWT classes use org.eclipse.swt as a prefix, in order to simplify the description, we use the * to represent org.eclipse.swt, such as the * .widgets package, represents org.eclipse.swt.widgets package.

Our most commonly used graphics components are basically included in the * .widgets package, such as Button, Combo, Text, Label, Sash, Table, and more. Two of these most important components are both shell and composite. The shell is equivalent to the main window framework of the application, and the above example code is to open an empty window in the application shell component. Composite is equivalent to the Panel object in Swing, which acts as the role of the component container. When we want to add some components in a window, it is best to use Composite as the container of other components, then go to * .Layout package to find one Suitable layout. SWT The layout of the component also uses the combination of Layout and Layout Data in Swing or AWT, and four layout can be found in the * .Layout package and the layout structure object corresponding to them can be found. Layout Data. In the * .custom package, the extension of some basic graphics components is included, such as CLABEL it is, is an extension of the standard Label component, which can be added to the text and images at the same time, or the border. StyleDtext is an extension of the Text component, which provides a rich textual function, such as the background color, foreground color or font settings for a certain text. A new STACKLAYOUT layout method can also be found in the * .custom package. SWT responds to user operations, such as mouse or keyboard events, and Observer mode in AWT and SWING, which can find the listner interface of the event listener and the corresponding event object in the * .event package, such as the common mouse event listener interface. MouseListener, MouseMovelistener and MouseTrackListener, and the corresponding event object MouseEvent.

* .Graphics package can find APIs for pictures, cursors, fonts, or drawings. For example, you can call different types of image files in the system. Drawing functionality of the picture, component or display is implemented via the GC class.

For different platforms, Eclipse has also developed some targeted APIs. For example, in the Windows platform, you can easily call the OLE control through the * .ole.win32 package, which makes the Java program embedded in IE browser or Word, Excel.

To learn more about SWT, you can find the Javadoc description of the SWT in the Help document of the Eclipse IDE. Of course, the most in-depth understanding of the source code of SWT, which is also the charm of the programmer!

More complex procedures Let us show a more complex program than the above example. This program has a text box and a button. When the user clicks the button, the text box displays a welcome information.

For text boxes and buttons have a relatively reasonable size and layout, a gradlayout layout is used. This layout is the most commonly used in SWT and the most powerful layout, almost all formats may be reached through gradlayout. The following procedure also involves how to apply system resources (color), and how to release system resources.

PRIVATE VOID INITSHELL (shell shell) {

/ / Set the layout object for the shell

GridLayout gshelllay = new gridlayout ();

Shell.setLayout (gshelllay);

/ / Construct a container for a Composite component as a text box and a button

Composite Panel = New Composite (shell, swt.none);

/ / Specify a layout structure object for Panel. Here, Panel as much as possible, is the space of all application windows.

Griddata gpaneldata =

New griddata (griddata.grab_horizontal | griddata.grab_vertage); panel.setlayOutdata (gpaneldata);

/ / Set a layout object for Panel. Text boxes and buttons will be displayed in this layout object.

GridLayout gpanellay = new gridlayout ();

Panel.setLayout (gpanellay);

/ / Generate a background color for Panel

Final Color Bkcolor = New Color (Display.getCurrent (), 200, 0,200);

Panel.setBackground; BKColor

/ / Generate text box

Final text text = new text (panel, swt.multi | swt.wrap);

/ / Specify a layout structure object for the text box, where the text box is as much as possible with the space of Panel.

GridData gtextdata =

New GridData (GridData.grab_horizontal | griddata.grab_vertage | griddata.fill_both;

Text.setLayOutdata (gtextdata);

/ / Build button

Button Butt = New Button (Panel, SWT.PUSH);

Butt.Settext ("Push");

/ / Specify the mouse event for the button

Butt.addmouselistener (new mouseadapter () {

Public void mousedown (MouseEvent E) {

// Show information when the user clicks the button

Text.Settext ("Hello Swt");

}

});

// The Disposelistener is triggered when the main window is turned off. Here is used to release the background color of the Panel.

Shell.adddisposelistener (new disposelistener () {

Public void widgetdisposed (DisposeEvent E) {

Bkcolor.dispose ();

}

});

}

Join the method INITSHELL () in this code into the first Open an empty window, get a full GUI application that can run successfully. The operation method can refer to the first example.

The management of system resources is in a graphical operating system, and there are resources in the system, such as pictures, fonts, colors, and more. Usually these resources are limited, the programmer must be very careful to use these resources: When you use them, please release it as soon as possible, otherwise the operating system will sooner will have the oil, and have to restart, more serious will result in system breakdown.

SWT is developed by Java. The great advantage of Java language itself is JVM's "garbage recycling mechanism", and programmers usually do not have to pay the release of variables, memory recycling. Then, for SWT, is the operation of system resources? The answer is a bad news, a good news.

The bad news is that SWT does not use JVM's garbage collection mechanism to deal with the resource recovery problem of the operating system. A key factor is because JVM's garbage collection mechanism is uncontrollable, that is, the programmer can't know, it is impossible to do it. Let JVM recycle resources at a moment! This is fatal on system resources. Imagine your program hopes to see tens of thousands of pictures in a loop statement, the regular processing method is to release one, then view, then release the image resource, then Turn the next picture in the loop, this is for the operating system, and only the program is only a resource of one image. But if this process is completely handed over to JVM dealing, it may be after the end of the loop statement, the JVM will release the image resource, and the result may be that your program has not yet run, the operating system is over. But the following good news may make this bad news becomes irrelevant. For SWT, just understand two simple "gold" rules, you can use system resources! The reason is called the gold rule, one is because few, only two, the second is because they are surprisingly simple. The first one is "who occupies, who releases", the second is "the parent member is destroyed, the sub-component is also destroyed". The first principle is a principle without any exception, as long as the program calls the constructor of the system resource class, the program should care about this system resource at a moment. For example, call

Font Font = New Font (Display, "Courier", 10, SWT.NORMAL

Then you should call when you need this Font.

Font.dispose ();

For the second principle, it means that if the program calls the Dispose () method of a component, the sub-component of all this component is also destroyed by the DISPOSE () method. The relationship between the sub-member and the parent member referred to herein is formed when the constructor of the calling member is formed. such as,

Shell shell = new shell ();

Composite Parent = New Composite (shell, swt.null);

Compositive child = new Composite (parent, swt.null);

The parent of which Parent is the shell, and the shell is the main window of the program, so there is no corresponding parent member, and the Parent includes the Child sub-component. If you call the shell.dispose () method, apply the second rule, then the Dispose () method of the PARENT and Child components will be automatically called by the SWT API, and they also destroy.

For these two rules, the articles in Reference 2 have a more in-depth introduction.

Thread Problem In the GUI system of any operation platform, the access operation of the component or some graphics API is strictly synchronized and serialized. For example, the button member in a graphical interface can be set into an enabled or disabled state (disable), the normal processing method is that the user is placed in the Event processing queue of the GUI system. (This means that the access operation is serialized), then processes (this means that the access operation is synchronized). Imagine When the set function of the button can be used, the program wants to set the button again to disable the status, which will inevitably cause a conflict. In fact, this operation will trigger an exception in any GUI system.

The Java language itself provides a multi-threaded mechanism, which is unfavorable to the GUI programming, which does not guarantee synchronization and serialization of the graphics component operation. SWT uses a simple and straightforward way to adapt to the local GUI system to the thread: in SWT, there is usually a unique thread called "user thread", only in this thread, can call the component or some The access operation of the graphics API. If the program is called directly in the non-user thread, then SwTexcepiton will be thrown. However, SWT is also available in the * .widget.display class, two methods can be indirectly available to the access operation of the graphics component of the non-user thread, which is the two methods of syncexec (runnable) and asyncExec (runnable) to implement of. For example: // This time the program runs in a non-user thread and wants to add a button on the component panel.

Display.getCurrent (). AsyncExec (New Runnable () {

Public void run () {

Button Butt = New Button (Panel, SWT.PUSH);

Butt.Settext ("Push");

}

}); Methods Syncexec () and asyncexec () The difference is that the former is returned after the specified thread execution is completed, and the latter will return to the current thread whenever the specified thread is executed.

SWT extension: JFaceJFace and SWT is better than the relationship between Microsoft's MFC and SDK, JFACE is based on SWT development, and its API is easier to use than SWT, but the function is not SWT. Direct. For example, the following code Apply the MessageDialog in the JFACE Opens a Warning dialog:

MessageDialog.Openwarning (Parent, "Warning", "Warning Message");

If you only complete the above features with SWT, the statement will not be less than 30 lines!

JFACE is originally a set of APIs written for more convenient use of SWT, which is the main purpose to develop an Eclipse IDE environment, not to apply to other independent applications. So before the Eclipse version 2.1, it is difficult to peel out the JFACE API completely from the Eclipse's kernel API. It always imports more less than the Eclipse core code class or interface other than JFAce can get a JFACE without any compilation. Develop bag. However, the Eclipse organization seems to have gradually realized that JFACE's important role in developing an independent application. In the 2.1 version developed, JFACE has also begun to become a completely independent development package as SWT, but this development package is also In the change (the author's Eclipse2.1m3 version) is applied. The packet prefix for the JFACE development package is starting with org.eclipse.jface. The JAR package and source code are also like SWT, as well as to your Eclipse installation path} / plugins path.

For developers, when developing a graphics component, a better way is to find a first to JFACE packages, see if there is a more concise implementation method, if you don't use the SWT package to achieve it. For example, JFACE provides a good support for the dialog box, except for various types of dialogs (such as the MessageDialog with the top, or with a Title Bar), if you want to implement a custom dialog, it is best to The Dialog class in JFACE inherits, not from * .widget.dialog in SWT.

Applying the classes in the Preference package in JFACE easy to make your own software a very professional configuration dialog. For Tree, Table and other graphics components, they also need to be associated with data, such as data displayed in Table, providing this type of model-view mode for this type of component in the View packet in JFACE, this method Separate display and data, more development and maintenance. The most features in JFACE are the processing of text content. Dozens of dozens of text processing related classes can be found in org.eclipse.jface.text. *. More coming with an application Java program is usually published in a Class file, running the Class requires JRE or JDK support. This is another fatal weakness of the Java GUI program, imagine that the app for the majority of users, no matter how simple your program features, or your code is very streamlined, you have to let users go Download a 7, 8M JRE, that is frustrating. And for programmers, Class usually means exposure to source code, anti-compilation tools make those people who are tuned easily to get your source code. Although there are many encryption methods for Class, it is always expensive for sacrifice performance. Fortunately, there are other ways available: compile the class into an EXE file!

Reference 3 provides a detailed approach to applying GCJ to compile SWT into an EXE file. As a supplement to that article, it is currently available to GCJ under Windows by downloading Cygwin. Excelsior is also a very good compiler that the author used, the only regret is its price!

The advantages of the SWT development kit, simple, cross-platform, reliable, and other Java language itself are gradually blended into applications developed to the graphical interface. Therefore, I believe that the door of another success in the Java language is gradually opening.

Reference:

Http://www.e2one.com Polar Software Studio Apps SWT & JFACE API Developed Enterprise Real-Time Communication IM software, is an example of SWT & JFACE develops a Standalone program.

Http://www.eclipse.org/Articles/swt-design-2/swt-design-2.html This article details how SWT developers manage system resources.

http://www-900.ibm.com/developerworks/cn/linux/guitkit/j-nativegui/index.shtml describes how to apply the SWT program to GCJ compile the cost of the machine application file.

http://www-900.ibm.com/developerWorks/cn/java/l-eclipse/index.shtml introduces the use of Eclipse and the simple plugin development process.

Www.eclipse.org All about Eclipse, SWT, JFACE can find the answer here. It is also a website to download the Eclipse development environment.

Http://www.cygwin.com/ You can get the Windows version of GCJ.

Regarding the author Ni Dapeng, many years of software development experience, which concentrated in Java related technologies in the past four years. You can contact him through e-mail: ndp@e2one.com.

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$

Second, ECLIPSE installation and configuration

First, you have to determine that you have already installed JDK in your operating system. If you haven't, please go to http://java.sun.com/j2se/1.4.2/download.html to download with your current operating system JDK.

Now we think that you are watching this article, you have installed JDK (I use JDK1.4.2 on my machine), we also need the following documents: Eclipse-sdk.2.1.1-win32.zip, Eclipse2.1.1 -SDK-WIN-LANGUAGEPACKFEATURE.ZIP, Gef-sdk-i20030618.zip, please go to http://www.eclipse.org to download; additional, SWT-Designer-free-1.11.zip Please http: //www.swt-designer.com go to download. The above compressed files will be released all over the directory, except for Eclipse-sdk.2.1.1-win32.zip, the plugins directory and the Feature directory in the directory are copked to Eclipse-SDK. 2.1.1-WIN32.Zip The Eclipse under the destined target folder is overwritten. Then to copy this Eclipse directory to the target path you want to install, run Eclipse.exe under Eclipse.

Because we installed Eclipse 2.1.1-sdk-win-languagePackfeature.zip, you will see a completely cultural Eclipse, including help, so you have any difficulties in using eclipse this IDE, Chinese Don't say more.

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

New Post(0)