Step-by-Step use SWT to develop Gui

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.

  1. Run eclipse, switch to the Java perspective, and create a Java project from package Development E. Name: Test
  2. 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.
  3. 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 ();
    }
    }
  4. 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.
  5. 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.