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 stability in SWT performance depends on the stability of the corresponding operating system graphics component.
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.
First SWT program
Let's 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 SWT API
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!