Develop a Java-based graphical user interface

Source: Internet
Author: User

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. The name is 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: eclipsepluginsorg. eclipse. swt. win32_3.0.0wswin32swt.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 = For example, the DLL address on my machine is D: eclipsepluginsorg. eclipse. swt. win32_3.0.0oswin32. 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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.