Download the SWT package and introduce the SWT package (a simple SWT program instance and a detailed explanation)

Source: Internet
Author: User

Let's start with a simple helloworld application.

 

You can download the SWT package on the eclipse website.

 

Go to the address below
Http://www.eclipse.org/downloads/download.php? File =/Eclipse/downloads/drops/S-3.4M4-200712131700/swt-3.4M4-win32-win32-x86.zip
PointDownload from:[China] actuate Shanghai (HTTP ).

Or directly use the address below to download it.
Http://download.actuatechina.com/eclipse/eclipse/downloads/drops/S-3.4M4-200712131700/swt-3.4M4-win32-win32-x86.zip

 

After downloading, pull is used. How can I deploy it to my own project? Continue to look down

 

  1. Add SWT to the class path of the project

First, create a Java project. Before using SWT, you must add the SWT library file to the class path of the project. The procedure is as follows:

 

1. Download SWT. On the eclipse SDK download page, you can download an independent version of SWT. Find the topic marked with SWT binary and source. Download the version suitable for your operating system and save it to your hard disk Without decompression.

2. On the eclipse menu bar, select File-> Import... to open the import wizard.

3. Select existing projects into workspace and click Next.

4. Select archive file and use the Browse... button to find the SWT file you just downloaded.

5. Click Finish to complete SWT import.

6. Right-click the project you created and select Properties to open the Properties dialog box.

7. Select the Java build path tab and click Add.-> Projects

8. Select the org. Eclipse. SWT project and click OK to add the SWT library to your project (see figure 1 ).

 

Figure 1

 

  Ii. SWT code

Now, in your Java project, create a new Java file named helloworld. The main () method overwrites the following code in the helloworld. Java file:

1 public static void main (string [] ARGs ){
2 display = New Display ();
3 shell = new shell (Display );
4 shell. settext ("Hello World ");
5 shell. setbounds (100,100,200, 50 );
6 shell. setlayout (New filllayout ());
7 label Label = new label (shell, SWT. center );
8 label. settext ("Hello World ");
9 color red = new color (display, 255, 0, 0 );
10 label. setforeground (red );
11 shell. open ();
12 while (! Shell. isdisposed ()){
13 if (! Display. readanddispatch () display. Sleep ();
14}
15 red. Dispose ();
16 display. Dispose ();
17}

Note:

After overwriting the above Code, select the source-> organize imports command (or press Ctrl + Shift + O) in the menu bar to import the SWT package to the helloworld. Java file.

The following is a detailed explanation of each line of code:

Row 2nd: Each SWT-based application has a display class instance. It is used to link the underlying platform to the SWT. In addition to managing SWT event loops, you can also access platform resources required by SWT. In lines 16, the display instance is submitted to the garbage collector.

Row 3: each window has a shell window framework to interact with users. Shell processes action behaviors like all Windows systems and acts as a place where window controls are placed.

Row 4th: settext () method to set the title of the window.

Row 5th: setbounds () method to set the window size and position. in this example, set the window to 200 pixels in width, 50 pixels in height, and place it at a location of X pixels away from the upper left corner of the screen.

Row 6th: The setlayout () method sets the layout of the window framework. filllayout and the full layout manager, so that the component size will be filled with the layout manager of the entire container. SWT as much as possible.

Row 7th: create a simple label component on the shell and center the text content of the label.

Row 3: Set the text content of the label in the settext () method.

Row 9th: creates an instance of the red color class. You can also use the following statement to obtain the system red instance:

Color red = display. getsystemcolor (SWT. color_red );

Row 10th: setforeground () method to set the foreground color of the label

Row 11th: The Window framework is invisible till now. Use the open () method to make the window visible.

Row 12th: whether the while statement loop detection window is closed.

Row 13th: display controls the loop. readanddispatch () method of the event to read the event from the event queue on the platform, and assign them to the appropriate handler (receiver ). As long as there are always events in the queue that can be processed, this method returns true all the time. When the event queue is empty, false is returned (therefore, the UI thread on the user interface is allowed to stay in sleep state until the event queue is not empty ).

Row 15 and 16: when the window is closed cyclically, you need to release the color, display, and related platform resources. Note that the system color instance (colors) will be submitted for release.

  Ii. Run this example

Generally, the run as-> JAVA application command is used to start a Java application. In this case, an unsatisfiedlinkerror exception is thrown, indicating that the local SWT library is not found. If you run the run as> SWT Application Command, the swt startup configuration window is displayed, as shown in Figure 2. You can click the run button in Figure 2.

 

Figure 2

 

Click Run. Figure 3 shows the running result.

 

Figure 3

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.