SWT (Standard Widget Toolkit) is a "Java-based" graphical interface development Library launched by IBM, the reason why I say it is "Java-based" means that programmers use the Java language when writing code. In fact, the underlying implementation of SWT is completed in C language. But these are transparent to programmers.
When we use SWT to develop a GUI program, we use swt api to write it directly. In fact, many Java code is implemented by removing the C code through JNI. Each class on different platforms has different implementation methods. This article does not aim to describe SWT design principles. If you are interested in these methods, refer to http://www.eclipse.org/articles/article-swt-design-1/swt-design-1.html.
The following describes how to use SWT. First, I assume that you have installed eclipse3.0. Of course, other versions are also supported. If not, download it from www.eclipse.org.
- Run eclipse, switch to the Java perspective, and create a Java project from package Development E. Name: Test
- In libraries, select Add external jars to add the class library required to run SWT. This is related to the system. For example, in Windows XP, the address is D: /Eclipse/plugins/org. eclipse. SWT. win32_3.0.0/WS/Win32/SWT. jar. Add it in. We recommend that you set variables to point to the SWT. jar file, and then add variables directly.
- Write Java code, such
Import org. Eclipse. SWT. Widgets .*;
Import org. Eclipse. SWT .*;
Public class swthello {
Public static void main (string [] ARGs ){
Display display = New Display ();
Shell shell = new shell (Display );
Label Label = new label (shell, SWT. None );
Label. settext ("Hello, world! ");
Shell. Pack ();
Label. Pack ();
Shell. open ();
While (! Shell. isdisposed ())
If (! Display. readanddispatch ())
Display. Sleep ();
Display. Dispose ();
Label. Dispose ();
}
}
- Configure the runtime environment. Because SWT needs to use local resources when running the program, if you run the above program now, an error will occur, similar to Java. lang. unsatisfiedlinkerror: No swt-win32-2133 in Java. library. path ", so you must specify the location of the required DLL. The specific method is to switch from the menu run-> run to the running configuration interface, select arguments to write-djava In the VM arguments. library. path = <folder containing the swt dll> for example, the DLL address on my machine is D:/Eclipse/plugins/org. eclipse. SWT. win32_3.0.0/OS/Win32/x86. In this way, it is a little troublesome to configure each operation. Therefore, we recommend that you add the path including DLL in the environment variable path.
- Run the program and you will see the effect of your first program :) I feel pretty good, and the speed is faster than Swing/AWT.
For more information about how to use SWT, see the relevant articles at www.eclipse.org. Read more awt api and write more code!