Basic SWT widegts 1st

Source: Internet
Author: User

The first time I translated other people's things, I had a lot of advice on the shortcomings, but I did not know much about it. 

Basic SWT widegt

By Shantha Ramachandran

Department of Computer Science, Uniersity of Manitoba, Winnipeg, Canada

Overview:

This document describes the basic SWT (standed widget tollkit) widget (Soso said: it can also be called a widget, a widget, but I am willing to call it a widget ). We will focus on using SWT to develop Java programs, rather than using plug-ins to expand the eclipse platform. If you are not ready to use the eclipse platform to run a basic SWT program, you can look back at the previous sections on installing eclipse and the SWT library. (Soso said: You can go to www.eclipseworld.org to check it. If you have any questions, I will go there and post it. It is helpful .)

Widget structure:

The basic components of widgets are described here. To create a program using SWT widgets, You need to import the following packages at the beginning of the program:

Org. Eclipse. SWT .*;

Org. Eclipse. SWT. widegts .*;

To create a SWT widgets program, you must create a display and a shell object:

Display display = New Display ();

Shell shell = new shell (Display );

Display is an object that contains all gui components. Display is invisible, but the components added to it are visible. Generally, a program only needs a display object. Shell is the window in the program. You can create a lot of shells in a program. These shells can be attached to the display (Main Window) of the top level or other shells (subwindows ). This is not the same as the swing widget. SWT's widgets property is initialized or modified in a similar way. You can set the shell size as follows:

Shell. setsize (100,100 );

After initialization, you need to open the shell (call the open method) and then execute an event loop. This event loop is used to display the shell on the screen. You can place this code anywhere after the initialization shell and widgets. When the shell on the outermost layer is closed (the shell. Close method can be called), display must call the dispose method to release resources.

Shell. open ();

While (! Shell. isdisposed ())...{

If (! Display. readanddispath ())

Display. Sleep ();

}

Display. Dispose ();

Now you can add any widgets to your shell. We don't need layout manager (layout manager), but it is a little complicated to create widgets using layout manager, in the following example, layout manager is not required. I will introduce it in detail later.

Label:

Label is used to display some text. Generally, users cannot change its content (of course, programmers can ).

Use the following code to put a label on your shell:

Label label1 = new label (shell, SWT. Border );

Labels can be used in the following formats:Border,Center,Left,Right,WarpAndSeparator. Specifically, the separator style is special, which enables the label to be displayed as a separator. And separator styles can be set together.Horizontal,Vertical,Shadow_in,Shadow_outAndShadow_none.

The following code creates five labels, two of which are separators. The first text label has a border, the second is a common label, and the third is a label that sets the background color through the setbackground () method. The two separators in this example use a parallel style. The difference is that the first one adds a shadow style.

Label label1 = new label (shell, SWT. Border );
Label1.settext ("see no edevil ");
Label1.setsize (100,20 );
Label1.setlocation (30, 30 );
Label sep1 = new label (shell, SWT. Separator | SWT. Horizontal | SWT. shadow_in );
Sep1.setbounds );
Label label2 = new label (shell, SWT. None );
Label2.settext ("Hear No edevil ");
Label2.setsize (100,20 );
Label2.setlocation (30, 90 );
Label sep2 = new label (shell, SWT. Separator | SWT. Horizontal );
Sep2.setbounds (30,120,100, 20 );
Label label3 = new label (shell, SWT. None );
Label3.setsize (100,20 );
Label 3.setlocation (30,150 );
Label3.setbackground (new color (display, 200,111, 50 ));
Label3.settext ("Speak no edevil ");

The following figure shows the displayed image:

Text:

The text widget contains a text area that you can modify.

Run the following code to place a text widget on shell:

Text text1 = new text (shell, SWT. Border );

It supports the following styles:Border,H_scroll,V_scroll,Multi,Single,Read_onlyAndWrap.

Text widgets can set interesting attributes. If your text can be edited, you can set the maximum number of input characters:

Text1.settextlimit (30 );

You can also use the setechochar () method to create a password-style text. The parameters in this method are the characters that you want to replace the displayed password. The input string is recorded in text, but from the external perspective, you can only see a queue of strings that you set to replace the displayed characters:

Text2.setechochar ('*');

The following code creates three text widgets. The first one has a limit of 30 input characters. The second one displays all input characters as a group of asterisks (*), as shown in the password area. The last one uses seteditable () the method is set to read-only. Another way is to specifySWT. read_onlyAttribute.

You can try to run the following code and enter some characters in the text area:

Text text1 = new text (shell, SWT. Border );
Text1.settext ("type something in here ");
Text1.setbounds (, 20 );
Text1.settextlimit (30 );
Text text2 = new text (shell, SWT. None );
Text2.setechochar ('*');
Text2.setbounds (, 20 );
Text2.settext ("password ");
Text text3 = new text (shell, SWT. Border | SWT. h_scroll | SWT. v_scroll );
Text3.setbounds (10, 90, 200,100 );
Text3.seteditable (false );
Text3.settext ("Bet you can't type in here ");

The shell looks like this:

Button:

A button is a widget that can be clicked by users to execute a specific process.

Run the following code to place a button on the shell:

Button button1 = new button (shell, SWT. Push );

You canSWT. PushReplace it with any button style supported by SWT, which includes:Push,Check,Radio,ToggleAndArrow. You can also useFlat,Border,Left,RightAndCenter.

Use the settext () method to set the text displayed by the button:

Button1.settext ("hello ");

To make the button visible, you must set its size and position. You can also use setbounds to set both the size and position:

Button1.setlocation (0, 0 );

Button1.setsize (100, 20 );

Button1.setbounds (10, 10,100,100 );

You can also set its style, background, image, and other attributes through the set method. To add an event processing button to your button, you must first import the events package:

Import org. Eclipse. SWT. Events .*;

The basic event processing of the button is selection event handler. This selection event handler is called only when the button is selected and pressed. Let's add a listener to the button to create event handler:

Button1.addselectionlistener (New selectionadapter ()){

Public void widgetselected (selectionevent e ){

System. Out. println ("button1 was clicked ");

}

});

Here is a very small example that combines all the code shown above. It creates a button. When it is pressed, "button1 was clicked" is displayed on the console, along with an arrow button and a button with toggle and flat styles.

Button button1 = new button (shell, SWT. Push );
Button1.settext ("Click me ");
Button1.setlocation (0, 0 );
Button1.setsize (100,20 );
Button1.addselectionlistener (New selectionadapter ()...{
Public void widgetselected (selectionevent e )...{
System. Out. println ("button1 was clicked ");
}
});
Button button2 = new button (shell, SWT. Arrow );
Button2.setsize (20, 20 );
Button2.setlocation (250,200 );
Button button3 = new button (shell, SWT. Flat | SWT. Toggle );
Button3.setsize (50, 50 );
Button3.setlocation (0,150 );

Legend:

Translate it here first. It seems very simple, that is, the format and other things are difficult to translate after a while (it may be a few hours later ). In fact, they are also basic things.

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.