Java Desktop application Design upstart: SWT Introduction

Source: Internet
Author: User
Tags event listener ole win32

The popularity of the Java language and its achievements in desktop applications (GUI programs) are clearly very inconsistent, and still rarely see very successful Java desktop programs. Although large software such as Jbuilder,netbean,jprobe is represented, it still does not prove that Java GUI programs are successful: their appearance is always out of tune with other software under the same operating system platform. The need for machine configuration also seems to be endless, making them tolerable only by the programs that always have the most high-performance PCs, or by professional users who don't care about money or time. For most computer users, AWT or swing represents a weird interface and an unacceptable speed. Standard Widget Toolkit (SWT) may be the end of the Java nightmare, the vast majority of Java programmers can finally develop efficient GUI programs, they have a standard appearance, few people can see that your program is written in Java, more importantly, These programs are cross-platform.

SWT itself is simply a set of low-level graphical interface APIs written by the Eclipse organization to develop the Eclipse IDE environment. It may have been unintentional or intentional, and so far, SWT has surpassed Sun's AWT and swing in both performance and appearance. Now that the Eclipse IDE has been developed to version 2.1, SWT has been very stable. The stability here should consist of two layers of meaning:

One refers to the stability of performance, the key is from SWT's design concept. SWT maximizes the operating system's graphical component API, which means that as long as the operating system provides the corresponding graphics widget, SWT simply applies jni technology to invoke them, only those components that are not provided in the operating system, and SWT does a simulation on its own. It can be seen that the stability of SWT performance depends on the stability of the corresponding operating system graphics component.

Another stability is that the names and structures of the classes and methods in the SWT API package have changed little, and programmers don't have to worry because the eclipse organization is developing very quickly (the Eclipse IDE has a nightly release every day), causing its own program code to change too much. From one version of SWT to another, it's usually easier to simply replace the SWT package.

First SWT Program

Now let's start with an SWT program. (Note: The following examples and instructions are primarily for Windows platforms, and other operating systems should be similar). To find the SWT package in the Eclipse installation file first, the Eclipse organization does not provide a separate SWT package download and must download the full Eclipse development environment to get the SWT package. SWT exists as a plug-in form of the Eclipse development environment, searching Swt.jar files in ${of your Eclipse installation path}\plugins path, and containing SWT's entire Java class file in the jar file found. Because SWT uses JNI technology, it also needs to find the corresponding JNI localization library file, because the version and the operating platform differ, the localization library file name will be somewhat different, such as Swt-win32-2116.dll is the window platform under the Eclipse build 2116 Dynamic Library, and the corresponding version of the library file on the UNIX platform extension should be. So, and so on. Note that eclipse is an open source project, so you can also find SWT's source code in these directories, which is believed to be useful for development. Here is a section of 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();
  Shell shell = new Shell(display);
  shell.open();
  // 开始事件处理循环,直到用户关闭窗口
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }
  display.dispose();
 }
}

Be sure to include the Swt.jar file in the classpath, and use Javac to compile the example program first. Compile error-Free to run java-djava.library.path=${your SWT local library file location path} Com.e2one.example.OpenShell, such as the swt-win32-2116.dll part of the path is C:\ Swtlib, the command to run should be java-djava.library.path=c:\swtlib Com.e2one.example.OpenShell. After a successful run, the system opens an empty window.

Profiling SWT APIs

Let's further analyze the composition of the SWT API below. All SWT classes are prefixed with ORG.ECLIPSE.SWT, and in order to simplify the instructions, we represent the prefix ORG.ECLIPSE.SWT with the * number, such as the *.widgets package, which represents the Org.eclipse.swt.widgets package.

Our most commonly used graphical components are basically included in the *.widgets package, such as button,combo,text,label,sash,table and so on. One of the two most important components is the number of shells and composite. The shell corresponds to the main window frame of the application, and the example code above is to open an empty window using the shell widget. Composite is equivalent to a panel object in swing, acting as a component container, and when we want to add some artifacts to a window, it is best to use composite as a container for other artifacts and then to *.layout the package to find an appropriate layout. SWT's layout of the widget also takes the form of a combination of Layout and Layout data in swing or AWT, where four types of Layout and the corresponding layout-structure objects (Layout data) can be found in the *.layout package. In the *.custom package, there are extensions to some basic graphic artifacts, such as the Clabel, which is an extension of the standard label widget, which can be added to both text and pictures, or borders. Styledtext is an extension of the text widget that provides rich text functionality, such as the background color, foreground color, or font settings for a paragraph of text. A new Stacklayout layout can also be found in the *.custom package.

SWT's response to user actions, such as mouse or keyboard events, is also based on the Observer mode in AWT and swing, where the listener interface and the corresponding event object that the event listens to can be found in the *.event package. For example, the common mouse event listener interface Mouselistener,mousemovelistener and Mousetracklistener, and the corresponding event object MouseEvent.

An API for pictures, cursors, fonts, or drawings can be found in the *.graphics package. For example, you can invoke different types of picture files in the system by using the image class. The function of drawing for a picture, widget, or monitor is realized through the GC class.

For different platforms, Eclipse has also developed a number of targeted APIs. For example, on the Windows platform, OLE controls can be easily invoked via the *.ole.win32 package, making it possible for Java programs to embed IE browsers or Word, Excel, and so on!

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.