SWT Common Controls Chinese tutorial

Source: Internet
Author: User
Tags object end final integer pack sleep string window
Tutorials | controls | chinese

  1, the configuration of SWT in eclipse

Recommended configuration: jdk1.4.2 and eclipse3.1

Before you call the SWT control in your code, you first establish a project and then select the project's properties-> Java build Path to add the standard Widget Toolkit to the Library page. As shown in the following illustration:

Next you can create the first Eclipse applet, create a new class, and in the corresponding code for that class, enter the following program, where package and class names determine the name according to the actual situation.

Package mypakage;
Import org.eclipse.swt.widgets.*;
Import org.eclipse.swt.*;
/* Import required Class Library * *
public class Myfrm1 {
Public Myfrm1 () {
Super ();
}
public static void Main (string[] args) {
Display display = new display ();
Shell shell = new shell (display);
/*shell as a Window object * *
Label label = new label (Shell, SWT. NONE);
Label.settext ("Hello, world!"); /* Create a Label object and set the caption text * *
Label.pack ();
Shell.pack ();
Shell.open (); /* Open and display windows * *

while (!shell.isdisposed ())
if (!display.readanddispatch ())
Display.sleep (); /* The display object is waiting until the window has not been destroyed.

Display.dispose (); /* Otherwise, destroy the object, release the resources occupied by the object.
Label.dispose ();
}
}

Running the above code (run-> debug-> SWT application) will produce a window that looks like this

  2, the Use of button

There are a number of possible types of buttons, such as:

Swt. BORDER a button that contains a border

Swt. Check checkbox

Swt. Push Normal button

Swt. RADIO radio button

3, the use of text

The type of text box also has a number of options, such as:

Swt. BORDER contains borders

Swt. READ_ONLY Read Only

The following illustration is a window that contains a button and a text box

The corresponding code for designing the above window is:

Package mypakage;
Import org.eclipse.swt.widgets.*;
Import Org.eclipse.swt.SWT;
Import org.eclipse.swt.events.*;
Import org.eclipse.swt.layout.*;
public class Myfrm1 {
Public Myfrm1 () {
Super ();
}
public static void Main (string[] args) {
Display display = new display ();
Shell shell = new shell (display);
Shell.setsize (300, 200);
Shell.setlayout (New Rowlayout ());
Shell.settext ("button Example");
Final button button = New button (Shell, SWT. BORDER);
Button.settext ("click Me");
Final text = new text (Shell, SWT. BORDER);
Shell.open ();

while (!shell.isdisposed ()) {
if (!display.readanddispatch ()) display.sleep ();
}
Display.dispose ();
}
}

If you want to set the location and size of the control precisely, you can use the SetBounds (x, y, Width, height) method to replace the Shell.setlayout (new Rowlayout ()). For example: Button.setbounds (80, 80, 90, 20);

Button monitoring and event handling

Code for button click event Handling:

Button.addselectionlistener (New Selectionlistener ()
{
public void widgetselected (Selectionevent event)
{
Text.settext ("No worries!");
}
public void widgetdefaultselected (Selectionevent event)

{
Text.settext ("No worries!");
}
});

Before adding the above code to Shell.open, the following effects occur when the button is clicked:


Analysis: Because of the addition of a listener for button buttons, the button is "monitored" at all times and the text control is assigned "No worries" when the button control is selected (click) to select the event.

According to the principle of the listening event, design the following program, which can get the x coordinates of the mouse and display in the text box:

Package mypakage;
Import org.eclipse.swt.widgets.*;
Import Org.eclipse.swt.SWT;
Import org.eclipse.swt.events.*;
Import org.eclipse.swt.layout.*;

public class Myfrm1 {
Public Myfrm1 () {
Super ();
}
public static void Main (string[] args) {
Display display = new display ();
Shell shell = new shell (display);
Shell.setsize (300, 200);
Shell.setlayout (New Rowlayout ());
Final text = new text (Shell, SWT. SHADOW_IN);

Shell.addmousemovelistener (New Mousemovelistener ()
{
public void MouseMove (MouseEvent e)
{
Integer y=new integer (e.x); /* Converts x coordinates to an object of type integer * *
Text.settext (Y.tostring ());
}
});

Shell.open ();
while (!shell.isdisposed ()) {
if (!display.readanddispatch ()) display.sleep ();
}
Display.dispose ();
}
}

Listening mode:

Controllistener for handling movement and dimensional change

Focuslistener is used to handle getting focus and losing focus

KeyListener Processing Key input

MouseListener, Mousemovelistener, mousetracklistener to handle the mouse action

Selectionlistener handles selection behavior of controls (including button clicks)

Note: The listening mode is associated with the event that it can handle, and the listening mode determines the kind of event that can be handled, for example:

Shell.addmouselistener (New MouseListener ()
{
public void MouseMove (MouseEvent e)
{Text.settext ("MouseMove");}
public void MouseDoubleClick (MouseEvent e)
{Text.settext ("MOUSEDBCLC");}
public void MouseDown (MouseEvent e)
{}
public void MouseUp (MouseEvent e)
{}
});

You will find that when the mouse moves, the Text.settext ("MouseMove") is not always able to execute, and MouseDown, MouseUp events cannot be omitted because MouseListener can only handle MouseDoubleClick , MouseDown, MouseUp three kinds of events, and these three kinds of events can not be separated.

3, List control

The style of the list control includes:

Swt. BORDER contains borders

Swt. H_scroll contains a horizontal scroll bar

Swt. V_scroll contains vertical scroll bars

Swt. Single Allow Select

Swt. MULTI Allow check

To create a list containing 11 elements, you can use the following code to implement the

Final list = new List (shell, SWT. Single);
for (int i=0;i<=10;i++)
List.add ("item" +i);

The following example can determine the options selected in the list control, and the output is displayed in the console:

Package mypakage;
Import org.eclipse.swt.widgets.*;
Import Org.eclipse.swt.SWT;
Import org.eclipse.swt.events.*;
Import org.eclipse.swt.layout.*;
public class Myfrm1 {
Public Myfrm1 () {
Super ();
}
public static void Main (string[] args) {
Display display = new display ();
Shell shell = new shell (display);
Shell.settext ("List Example");
Shell.setsize (300, 200);
Shell.setlayout (SWT, New filllayout. VERTICAL));
Final list = new List (shell, SWT. BORDER | Swt. MULTI | Swt. V_scroll);
for (int loopindex = 0; Loopindex < loopindex++) {
List.add ("Item" + loopindex);
}
List.addselectionlistener (New Selectionlistener ()
{
public void widgetselected (Selectionevent event)
{
int selections[] = list.getselectionindices ();
String outtext = "";
for (int loopindex = 0; Loopindex < selections.length;
loopindex++) Outtext + + + Selections[loopindex] + "";
System.out.println ("You selected:" + Outtext);
}
public void widgetdefaultselected (Selectionevent event)
{
int [] selections = List.getselectionindices ();
String outtext = "";
for (int loopindex = 0; Loopindex < selections.length; loopindex++)
Outtext + = Selections[loopindex] + "";
System.out.println ("You selected:" + Outtext);
}
});
Shell.open ();
while (!shell.isdisposed ()) {
if (!display.readanddispatch ()) display.sleep ();
}
Display.dispose ();
}
}

Effect Chart:

You selected:4 5 6 7 8 9 10

Analysis: The List.getselectionindices () method will obtain a collection of selected items, selections[] or [] elections represents a dynamic one-dimensional array.

4. Menu control

The general steps for creating a menu are:

1, in the establishment of the menu, first need to create a menu bar, you need to use the Swt.bar property

Menu MenuBar = new Menu (Shell, SWT. BAR);

2, on the basis of the menu bar, create a drop-down menu of the corresponding top-level menu items, you need to use the Swt.cascade property
Filemenuheader = new MenuItem (MenuBar, SWT. CASCADE);

Filemenuheader.settext ("&file");

3. Create a Pull-down menu related to top level menu items

dropMenu1 = new Menu (Shell, SWT. Drop_down);

4. Associate the top-level menu item with the Drop-down menu

Menuheader1.setmenu (DROPMENU1);

5. Add a submenu item for the Drop-down menu

dropitem1= New MenuItem (dropMenu1, SWT. PUSH);
Dropitem1.settext ("open");
...
...

6. Finally, specify the menu bar you want to display in the window

Shell.setmenubar (MenuBar);

Menu monitoring and Events

Reference button monitoring and events, design the following program, when clicked "Open" under the File submenu, display "Click Open menu!" in the text box

Dropitem1.addselectionlistener (New Selectionlistener ()
{
public void widgetselected (Selectionevent event)
{
Text.settext ("Click Open menu!");
}
public void widgetdefaultselected (Selectionevent event)
{
Text.settext ("Click Open menu!");
}
});

  5, using the toolbar Toobar

The toolbar can be created as follows: ToolBar ToolBar = new ToolBar (Shell, SWT. NONE);

Create a Toolbar child button on top of the toolbar and set the child button's caption:

Toolitem item1 = new Toolitem (toolbar, SWT. PUSH);
Item1.settext ("Item1");

For example:

ToolBar ToolBar = new ToolBar (Shell, SWT. NONE);
Toolitem item1 = new Toolitem (toolbar, SWT. PUSH);
Item1.settext ("Item1");
Toolitem item2 = new Toolitem (toolbar, SWT. PUSH);
Item2.settext ("item2");

The monitor and event of the tool bar

Instance: Creates a listener object, applies the listener to each button, and ultimately determines which button is clicked by the mouse, and the effect is shown below.

Listener Listener = new Listener () {
public void Handleevent (event event) {
Toolitem item = (toolitem) event.widget;
String string = Item.gettext ();
Text.settext ("You selected:" + string);}
};
Item1.addlistener (SWT. Selection, listener);
Item2.addlistener (SWT. Selection, listener);
Item3.addlistener (SWT. Selection, listener);
Item4.addlistener (SWT. Selection, listener);

  6, the use of scroll bar slider

Scroll bars are divided into border, vertical, horizontal three types, using the Slider.setbounds method to specify the location of the scroll bar.

The scroll bars are able to handle events including:

Swt. Arrow_down down or right button is clicked

Swt. Arrow_up the left or up button is clicked

Swt. DRAG Slider button is being driven

Swt. End Slider arrives at endpoint

Swt. Home Slider arrives at start

Swt. The scroll bar below or to the right of the page_down is clicked

Swt. The scroll bar above or to the left of the page_up is clicked

Instance: Move a button position based on the position of the slider

Slider.addlistener (SWT. Selection, New Listener () {

public void Handleevent (event event) {

Switch (event.detail) {

Case SWT. ARROW_DOWN:button.setBounds (Slider.getselection (), 0,20,10);

Break

Case SWT. ARROW_UP:button.setBounds (Slider.getselection (), 0,20,10);

Break

Case SWT. DRAG:button.setBounds (Slider.getselection (), 0,20,10);

Break

Case SWT. END:button.setBounds (Slider.getselection (), 0,20,10);

Break

Case SWT. HOME:button.setBounds (Slider.getselection (), 0,20,10);

Break

Case SWT. PAGE_DOWN:button.setBounds (Slider.getselection (), 0,20,10);

Break

Case SWT. PAGE_UP:button.setBounds (Slider.getselection (), 0,20,10);

Break

}
}

});

  7, tree-shaped control

The method used by the tree control is to first create an object of type one, and then continue to extend the node on top of that object, as well as the child nodes of the extended node.

Final Tree tree = new Tree (shell, SWT. BORDER);

You can use the Tree.setsize method to change the size of a tree control. When creating a node, you need to indicate the name of the parent node on which the node is dependent, such as TreeItem item0 = new TreeItem (tree, 0), then item0 becomes a level 0 (top-level) node in the tree object.

The following program will generate 9 nodes based on the tree object:

Final Tree tree = new Tree (shell, SWT. BORDER);

Tree.setsize (290, 290);

for (int loopIndex1 = LoopIndex1 <= 2008; loopindex1++) {

TreeItem item0 = new TreeItem (tree, 0);

Item0.settext ("Year" + loopIndex1);

}

On the basis of the above examples, 12 nodes are expanded on the basis of each 0-level node:

for (int loopIndex1 = LoopIndex1 <= 2008; loopindex1++) {

TreeItem item0 = new TreeItem (tree, 0);

Item0.settext ("Year" + loopIndex1);

for (int loopIndex2 = 1; loopIndex2 <= loopindex2++) {

TreeItem item1 = new TreeItem (item0, 0);

Item1.settext ("Month" + loopIndex2);
}

}

  8, dialog Box dialog

The dialog box is a subform that relies on the main form, as shown in the figure.

For example, when you click a button in the main form, a dialog box pops up and the button displays "dialog is disposed" When you close the dialog box dialog

Display display = new display ();

Final Shell shell = new shell (display);

Shell.setsize (300, 200);

Shell.settext ("main");

Final button opener = New button (Shell, SWT. PUSH);

Opener.settext ("click Me");

Opener.setbounds (20, 20, 50, 25);

Final Shell Dialog = new Shell (shell, SWT). Application_modal |

Swt. Dialog_trim);

Dialog.settext ("dialog");

Dialog.setbounds (10,10,50,60);

Dialog.adddisposelistener (New Disposelistener () {

public void widgetdisposed (Disposeevent e) {

Opener.settext ("dialog is disposed");

}
});

Listener Openerlistener = new Listener () {

public void Handleevent (event event) {

Dialog.open ();

}

};

Opener.addlistener (SWT. Selection, Openerlistener);

Shell.open ();

while (!dialog.isdisposed ()) {

if (!display.readanddispatch ()) display.sleep ();

}

while (!shell.isdisposed ()) {

if (!display.readanddispatch ())

Display.sleep ();

}

Display.dispose ();



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.