Using SWT for interface design in eclipse

Source: Internet
Author: User
Tags object interface variable thread win32 window
Designed to use SWT for interface design in Eclipse SWT is a cross-platform GUI development toolkit developed by IBM. As for why IBM struggled to build a GUI toolkit on its own, rather than using the sun's existing Java GUI framework, composed of AWT, Swing, Java 2d, Java, and so on, that's a long story. (Remember to read in a BBS on the origin of swt jokes about the class of the post).   1. Why use SWT? SWT is a Cross-platform GUI development toolkit developed by IBM. As for why IBM struggled to build a GUI toolkit on its own, rather than using the sun's existing Java GUI framework, composed of AWT, Swing, Java 2d, Java, and so on, that's a long story.   (Remember to read in a BBS on the origin of swt jokes about the class of the post). Prior to SWT, Sun had provided a Cross-platform GUI development Kit AWT (Abstract windowing Toolkit). Although the AWT framework also uses a native window widget (native widgets), it has not been able to break through the LCD problem. The LCD problem has resulted in the loss of some of the main platform features.   If you don't understand (I don't understand), in other words, if platform A has a window widget (widgets) 1-40 and Platform B has a window widget (widgets) 20-25, then the Cross-platform AWT Framework can only provide the intersection of the two widget sets. To solve this problem, Sun has created a new framework. Instead of using the native window widget, the framework uses the Emulation window widget (emulated widgets). Although this method solves the problem of LCD and provides a rich set of widgets, it also brings new problems. For example, the interface appearance of swing applications no longer resembles the appearance of native applications. Although these swing applications have achieved maximum performance improvements in the JVM, they still have performance problems that their native counterparts do not have.   Also, swing applications consume too much memory, so swing is not suitable for small devices such as PDAs and mobile phones. IBM has tried to completely address the above issues with the AWT and swing frameworks. Finally, IBM created a new GUI library, which is SWT. The SWT framework accesses native window parts through JNI. If a widget cannot be found on the host platform, SWT automatically simulates it. 2. An SWT application consists of the basic components of an SWT application as a display interface (displays), a command interface (Shell, which enables commands to enter and initialize the run)) and Window widget (Widgets). Display is responsible for managing the event loop and controlling communication between the UI thread and other threads. A shell is a window that is managed by the operating system Window manager in an application.   Each SWT application requires at least one display and a shell instance greater than or equal to 1. Figure 1: SWT application from a different perspective the SWT application is shown from a different perspective in Figure 1. The diagram on the left is an inheritance graph of a simplified UI object. The middle diagram shows the container structure of the UI object (containment structure).   The diagram on the right is the appearance of the UI after it is created. If an application uses more than one thread, each thread uses a display object that is assigned to its own instance. The programmer can use the static method Display.getcurent () to get the current active instance of the display object. The shell is used to represent windows in a particular operating system. The shell can be maximized, minimized, or normalized. There are two types of shells. The 1th is a high-level shell, which is the child window of display, and it is also a main window. The 2nd class is the dialog shell, which relies on the presence of other shell windows. The shell window eventually becomes the type above, looking at the style bits that are passed to the Shell constructor when the shell is created. The default value for a shell is Dialogshell. That is, if you do not take parameters, the default is a dialog shell.   If you give the parameter a display object, the shell will be a high-level shell. The properties of some window parts must be set at the beginning of their creation. The properties of these window parts are the style bits that are described earlier. In SWT's class, the style bit is defined as a constant. For example, the button button = New button (shell,). You can use the or (or) operator "|" To set multiple style bits. For example, if you want to set a push button with a border, you need to pass Swt.push | Swt. Border as a style bit parameter.   3. Pre-SWT development environment settings for SWT application development, you need to add the SWT library to the Classpath (CLASSPATH) and set the necessary environment variables. First, you will find the Swt.jar library file in the Eclipse_home\eclipse\plugins\org.eclipse.swt.win32_2.1.0\ws\win32 directory. Note that the "org.eclipse.swt.win32_2.1.0" directory here is with ECLIpse version of the. If you can't find it, use the file search function. Then open the following window in turn project->properies->javabuildpath->libraries->add Variable-> Eclipse Home->   Extend adds the Swt.jar file to the classpath. Next, you definitely want to compile this SWT application. However, the running exception shown below will appear. This exception occurs because the Swt.jar library uses a native library.   You need to set the JAVA.LIBRARY.PATH environment variable to use the native library in Java. The output of the console (console) is as follows: Java.lang.UnsatisfiedLinkError:no swt-win32-2136 in Java.library.path at Java.lang.ClassLoader.lo Adlibrary (Unknown source) at Java.lang.Runtime.loadLibrary0 (Unknown source) at Java.lang.System.loadLibrary (Unknown S Ource) at the Org.eclipse.swt.internal.Library.loadLibrary (library.java:108) at Org.eclipse.swt.internal.win32.OS. (os.java:46) at Org.eclipse.swt.widgets.Display.internal_new_GC (display.java:1291) at   Org.eclipse.swt.graphics.Device.init (device.java:547) at Org.eclipse.swt.widgets.Display.init (display.java:1316) At Org.eclipse.swt.graphics.Device. (device.java:96) at Org.eclipse.swt.widgets.Display. (display.java:291) at Org.eclipse.swt.widgets.Display. (display.java:287) at COM.SWTDEsigner.trytry.main (trytry.java:24) Exception in thread "main" sets the Java.library.path variable in the following steps: Turn on run-> run...-> J Ava applicaton-> "Project"->arguments-> VM Arguments.   Enter-djava.library.path=c:\eclipse\plugins\org.eclipse.swt.win32_2.1.0\os\win32\x86 in "VM Arguments"   Note that you want to enter the path of your own swt.jar. Tip: Load Native libraries: If you need to load the native libraries that your application uses, you can use the Runtime.getPlatform.loadLibrary ("LibraryName") method.

Related Article

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.