SWT Memo

xiaoxiao2021-03-06  40

1. SWT

SWT is proposed by IBM, a package for user interface. She has a lot of similarities with Sun's Swing, so if she masters Swing, they will be very simple to learn SWT. SWT has been used in Eclipse, however, in more cases in Eclipse, we use the JFACE package for SWT. Therefore, if you want to develop Eclipse plug-in, we need to master SWT and JFACE first.

2. From the example

a) code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test1 {

Public static void main (String [] args) {

Display d = new display ();

Shell s = new shell (d);

S.SetText ("test");

Label L = New Label (S, SWT.NONE);

L.Settext ("Hello World");

l.pack ();

S.PACK ();

S.Open ();

While (! s.isdisposed ()) {

IF (! D.ReadandDispatch ()) {

D.sleep ();

}

}

D. Dispose ();

}

}

a) Runchart:

b) Description:

From the above procedure, we can get some SWT and Swing different places:

1. When we need to instantiate a control, we need to provide reference to the parent control in the constructor.

2. By constructor, we do not need to explicitly add () operations to organize the control tree.

3. We need to manually start the message loop (WHILE section).

4. At the end of the program, we need to manually release resources.

3. Control:

a) label:

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test1 {

Public test1 () {

INIT ();

}

Private vidinit () {

...

Label B1 = New Label (S, SWT.NONE);

B1.Settext ("Label 1");

B1.SetBackground (New Color (D, 0, 0, 255));

Label B2 = New Label (S, SWT.BORDER | SWT.CENTER);

B2.Settext ("Label 2");

Label B3 = New Label (S, SWT.WRAP);

B3.Settext ("Label 3");

Label B4 = New Label (S, SWT.SEPARATOR | SWT.HORIZONTAL);

...

}

Public static void main (String [] args) {

TEST1 T = New Test1 ();

}

}

Ii. Running:

Iii. Description:

1. When Label is instantiated, you need to provide a style parameter. From the later example, this style parameter is widely used in SWT. With the use of Style parameters, we can contact less subclass than in swings. . 2. The style parameters that Label can use are: Border, Center, Left, Right, Wrap, and Separetor. Border is used to add a border for the control; center, left, Right is used for alignment of Label content; WRAP is used to automatically wrap when the text content exceeds the maximum length of the Label. When using the Separator, you can use Horizontal, Vertical, Shadow_IN, Shadow_out, and Shadow_none.

b) Text:

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test2 {

Public test2 () {

INIT ();

}

Private vidinit () {

...

Text t1 = new text (s, swt.none);

/ **

* Equivalent to password input box

* /

Text T2 = New Text (s, SWT.BORDER);

T2.sechochar ('*');

T2.SetTextLimit (10); // Limit the maximum number of input characters is 10

/ **

* Cannot be wrapped, equivalent to TextField

* /

Text T3 = New Text (s, swt.single);

T3.SetText ("Hello World");

/ **

* Support for wrap

* /

Text T4 = New Text (s, SWT.MULTI);

T4.SETTEXT ("Hello World");

/ **

* Equivalent to Textarea

* /

Text t5 = new text (s, swt.h_scroll | swt.v_scroll);

/ **

* Support automatic wrap

* /

Text t6 = new text (s, swt.wrap);

...

}

Public static void main (String [] args) {

TEST2 T = New Test2 ();

}

}

Ii. Running:

Iii. Description:

1. The style parameters that Text can use are: Border, H_Scroll, V_Scroll, Multi, Single, Read_only, WRAP. Border is used to add a border; h_scroll and v_scroll are used to provide scrolling, indicating that Text can add multi-line content when using scrolling; Multi and Single are used to indicate multi-line or single line; read_only is used to read. WRAP is used to automatically wrap when the text content exceeds the length of the Text line.

c) Button

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test3 {

Public test3 () {

INIT ();

Private vidinit () {

...

/ **

* Ordinary button

* /

Button B1 = New Button (s, swt.push | swt.left);

B1.Settext ("Button 1");

/ **

* single button

* /

Button b2 = new button (s, swt.check);

B2.Settext ("Button 2");

/ **

* Radio button

* /

Button B3 = New Button (S, SWT.RADIO);

B3.Settext ("Button 3");

/ **

* Arrow button

* /

Button B4 = New Button (S, SWT.Arrow | SWT.BORDER);

B4.Settext ("Button 4");

/ **

* Toggle button

* /

Button B5 = New Button (S, SWT.Toggle);

B5.Settext ("Button 5");

...

}

...

}

Ii. Running:

Iii. Description:

1. Style you can use with: Push, Check, Radio, Toggle, Arrow, Flat, Border, Left, Right, and Center. Among them, the top 5 categories used to change the button; the last 5 appearance for changing the button.

2. Since SWT does not provide the concept of ButtonGroup similar to Swing, we need to handle mutual exclusion issues between buttons in manual way.

d) list

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test4 {

...

Private vidinit () {

...

/ **

* Multiple choice

* /

List l1 = new list (s, swt.multi | swt.h_scroll | swt.border);

L1.Add ("Item 1");

L1.Add ("Item 2");

L1.Add ("Item 3");

/ **

* Radio

* /

List l2 = new list (s, swt.single | swt.v_scroll | swt.border);

L2.SETITEMS (New String [] {"Item 1", "Item 2", "Item 3"});

...

}

...

}

Ii. Running:

Iii. Description:

1. STYLE that List can use: Border, H_Scroll, V_Scroll, Single, and Multi. SINGLE and MULTI are used to represent radio and multiple selection.

e) Combo

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test5 {

Private Combo C2 = NULL;

Public test5 () {

INIT ();

}

Private vidinit () {

...

/ **

* No edit box

* /

Combo C1 = New Combo (S, SWT.DROP_DOWN | SWT.READ_ONLY);

C1.SETITEMS (New String [] {"Item 1", "ITEM 2", "Item 3"});

Combo C2 = New Combo (S, SWT.SIMPLE | SWT.BORDER);

C2.SETITEMS (New String [] {"Item 1", "ITEM 2", "Item 3"});

/ **

* No option

* /

Combo C3 = New Combo (S, SWT.DROP_DOWN);

...

}

...

}

Ii. Running:

Iii. Description:

1. STYLE that Combo can use: Border, Drop_Down, Read_only, Simple. Drop_down represents only the drop-down box; Simple indicates that existing pull-down boxes have edit boxes.

f) Composite

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test6 {

...

Private vidinit () {

...

/ **

* Composite is a container, he is similar to JPANel

* /

Composite C = New Composite (S, SWT.BORDER);

C.SetBackground (New Color (D, 255, 0, 0));

Label L = New label (C, SWT.BORDER);

L.Settext ("Hello World");

...

}

...

}

Ii. Running:

Iii. Description:

1. Composite is a container, equivalent to JPanel in swing.

2. Composite can be used with: Border, h_scroll, and v_scroll.

g) group

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

PUBLIC Class test7 {

...

Private vidinit () {

...

Group g1 = new group (s, swt.border | swt.shadow_etched_in);

G1.Settext ("Group 1");

G1.SetBackground (New Color (D, 255, 0, 0));

Button B = New Button (G1, SWT.PUSH);

B.SetText ("Button");

...

}

...

}

Ii. Running:

Iii. Description:

1. Group is a subclass of Composite, so he is also a container, but it is the difference between composite, she added a box than Composite.

2. The style you can use is: border, shadow_etched_in, shadow_etched_out, shadow_in, shadow_out and shadow_none. 4. Event monitor

a) SelectionListener

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test1 {

...

Private vidinit () {

...

Button B = New Button (S, SWT.PUSH);

B.SetText ("Button");

B.AddSelectionListener (New SelectionAdapter () {

@Override public void widgetSelected (SelectionEvent E) {

System.out.Println ("Button Was Pressed");

}

});

...

}

...

}

Ii. Description:

1. The role of SelectionListener is the same as the ActionListener in Swing.

b) Keylistener

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test2 {

...

Private vidinit () {

...

Text T = New Text (s, SWT.BORDER);

T.AddKeylistener (new keyadapter () {

@Override public void keypressed (KeyEvent E) {

IF (E.CHARACTER == 'a') {

System.out.println ("a is forbidden");

// You can ban the input characters can be prohibited by the DOIT property.

e.doit = ​​false;

} else {

System.out.println ("Pressed" E.CHARACTER);

}

}

});

...

}

...

}

Ii. Description:

1. SWT does not provide a VK code that is not related to the platform similar to Swing.

2. We can constrain the contents of the input box through the DOIT property in KeyEvent. This is convenient than Swing, if we want to complete the same operation in Swing, you may need to get the corresponding module object in TextField, and operate.

c) MouseListener, MouseMovelistener, MouseTrackListener

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test3 {

...

Private vidinit () {

...

Button B1 = New Button (s, swt.push | swt.border);

B1.Settext ("Button 1");

B1.Addmouselistener (new mouseadapter () {

@Override public void mousedown (MouseEvent E) {system.out.println ("Pressed At (" E.x "," E.Y ")")

}

});

Button B2 = New Button (S, SWT.PUSH | SWT.BORDER);

B2.Settext ("Button 2");

B2.AddmouseMovelistener (new mousemovelistener () {

Public void mousemove (MouseEvent E) {

System.out.println ("Pressed AT (" E.x "," E.Y ")")

}

});

Button B3 = New Button (S, SWT.PUSH | SWT.BORDER);

B3.Settext ("Button 3");

B3.AddmouseTrackListener (new mousetrackadapter () {

@Override public void mousehover (mouseevent e) {

System.out.println ("Pressed AT (" E.x "," E.Y ")")

}

});

...

}

...

}

Ii. Description:

1. SWT adds MouseTrackerListener to monitor the events that mmelets enter, leave, and stay on the control.

d) FocusListener

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test5 {

...

Private vidinit () {

...

Button [] BS = New Button [3];

INT i = 0;

FOR (Button B: BS) {

B = New Button (S, SWT.PUSH);

B.SetText ("Button i);

B.AddfocusListener (new focusadapter () {

Public void focusgained (Focusevent E) {

System.out.println (E.GetSource () "Gains Focus");

}

});

B.AddtraverseListener (New TraverseListener () {

Public void keytravesed (TraverseEvent E) {

IF (e.detail == SWT.TRAVERSE_TAB_NEXT) {

System.out.Println ("Don't use tab to travelse.");

e.doit = ​​false;

}

}

});

i ;

}

...

}

...

}

Ii. Description:

1. FocusListener is notified after the control is obtained.

2. TraveSelistener gets notifications before the control gets the focus, and the Detail value through TraverseEvent can know what the control is to get the focus, and through the DOIT value, you can limit whether the control gets the focus. 5. Advanced control

a) TABLE:

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test1 {

Private Table Table = NULL;

...

Private vidinit () {

...

Table = new table (s, swt.single | swt.border | swt.h_scroll

| SWT.V_SCROLL | SWT.FULL_SELECTION;

Table.setHeadervisible (TRUE);

Table.SetLinesVisible (TRUE);

TableColumn name = new TableColumn (Table, Swt.center);

Name.setText ("name");

Name.SetWidth (100);

TableColumn sex = new TableColumn (Table, SWT.Center);

Sex.Settext ("SEX");

Sex.SetWidth (50);

TableColumn age = new TableColumn (Table, SWT.Center);

Age.Settext ("age");

Age.SetWidth (50);

TableItem I1 = New TableItem (Table, SWT.NONE);

I1.Settext (new string [] {"nick", "m", "24"});

TableItem I2 = New TableItem (Table, SWT.NONE);

I2.Settext (New String [] {"CEN", "M", "24"});

Table.addSelectionListener (New SelectionAdapter () {

Public void widgetselected (SelectionEvent E) {

TableItem Ti = Table.getSelection () [0];

For (int i = 0; i <3; i )

System.out.print (ti.gettext (i) "/ t");

System.out.println ();

}

});

...

}

...

}

Ii. Running:

Iii. Description:

1. Table You can use the Style value: Border, H_Scroll, V_Scroll, Single, Multi, Check, Full_Serection, and Hide_Serability. Where SINGLE, MULTI is used to support radio and multi-selection; Check is used to add a checkbox for the first column; Full_SELECTION is used to select the line in which the item is located in all the lines. Hide_selection is used to prohibit the item in the form.

IV. structural diagram:

Shell

TABLE

TableColumn

TableItem

b) TabFolder

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test2 {

...

Private vidinit () {

...

TabFolder TF = New TabFolder (S, SWT.BORDER);

Composite C1 = New Composite (TF, SWT.BORDER);

C1.setLayout (New RowLayout ());

Label B1 = New Label (C1, SWT.BORDER);

B1.Settext ("Label 1");

Composite C2 = New Composite (TF, SWT.BORDER);

C2.setLayout (New RowLayout ());

Label B2 = New Label (C2, SWT.BORDER);

B2.Settext ("Label 2");

TabItem Page1 = New TabItem (TF, SWT.NONE);

Page1.Settext ("Page 1");

Page1.setControl (C1);

TabItem Page2 = New TabItem (TF, SWT.NON);

Page2.Settext ("Page 2");

Page2.SetControl (C2);

...

}

...

}

Ii. Running:

Iii. Description:

1. STYLE that Tab can use: border.

IV. structural diagram:

CONTROL

Shell

TabFolder

TabItem

CONTROL

c) Slider, Scale and ProgressBar

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test3 {

Private slider slider = NULL;

Private scale scale = null;

PRIVATE PROGRESSBAR PB = NULL;

...

Private vidinit () {

...

Slider = New Slider (s, SWT.HORIZONTAL);

Slider.setminimum (0);

Slider.setmaximum (100);

Slider.addSerectionListener (New SelectionAdapter () {

@Override public void widgetSelected (SelectionEvent E) {

System.out.println ("Slider IS:" Slider.getSelection ());

}

});

Scale = New Scale (S, SWT.HORIZONTAL);

Scale.setminimum (0);

Scale.setmaximum (100);

Scale.addSelectionListener (New SelectionAdapter () {

@Override public void widgetSelected (SelectionEvent E) {

System.out.println ("Scale IS:" Scale.getSelection ());

});

PB = New ProgressBar (S, SWT.HORIZONTAL | SWT.SMOOTH);

Pb.setminimum (0);

Pb.setMaximum (100);

Pb.setSelection (75);

...

}

...

}

Ii. Running:

Iii. Description:

1. Slider, Scale, ProgressBar can use the style with Border, Horizontal, and Vertical.

d) menu:

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test4 {

...

Private vidinit () {

...

Menu bar = new menu (s, swt.bar);

S.SetMenubar (BAR);

Menuitem fmenu = new menuItem (bar, swt.cascade);

FMenu.Settext ("file");

Menu FileMenu = New Menu (s, SWT.DROP_DOWN);

FMenu.SetMenu (filemenu);

Menuitem Miopen = New Menuitem (FileMenu, SWT.PUSH);

Miopen.Settext ("& Open / TCTRL O");

Miopen.setAccelerator (SWT.CTRL 'O');

MenuItem MICLOSE = New Menuitem (FileMenu, SWT.PUSH);

MiClose.Settext ("& close");

Menuitem Emenu = New Menuitem (bar, swt.cascade);

EMENU.SETTEXT ("Edit");

Menu EditMenu = New Menu (s, swt.drop_down);

EMENU.SETMENU (Editmenu);

Menuitem MiCopy = New Menuitem (EditMenu, SWT.CHECK);

Micopy.Settext ("& Copy");

Menuitem Mipaste = New Menuitem (EditMenu, SWT.RADIO);

Mipaste.setText ("& Paste");

Miopen.addSelectionListener (new selectionAdapter () {

@Override public void widgetSelected (SelectionEvent E) {

System.out.println ("Open SELECTED");

}

});

Button B = New Button (S, SWT.PUSH);

B.SetText ("Button");

/ **

* Pop-up menu

* * /

Menu mpopup = new menu (s, swt.pop_up);

Menuitem Mifile = New Menuitem (MPOPUP, SWT.CASCADE);

MIFILE.SETTEXT ("File");

Menu PfileMenu = New Menu (S, SWT.DROP_DOWN); MIFILE.SETMENU (PfileMenu);

Menuitem Pmiopen = New Menuitem (PfileMenu, SWT.PUSH);

Pmiopen.Settext ("open");

Pmiopen.addSelectionListener (New SelectionAdapter () {

@Override public void widgetSelected (SelectionEvent E) {

System.out.println ("Open SELECTED");

}

});

Menuitem PMiCopy = New Menuitem (MPOPUP, SWT.PUSH);

PMiPy.Settext ("Copy");

B.SetMenu (MPOPUP);

...

}

...

}

Ii. Running:

Iii. Description:

1. Class related to the menu is only Menu and MenuItem. In order to make up the entire menu bar, we need to identify different controls through different style.

2. When MENU's style is BAR, it means that this menu is a menubar.

3. If you need to create a new menu, we need:

Menuitem fmenu = new menuItem (bar, swt.cascade);

FMenu.Settext ("file");

Menu FileMenu = New Menu (s, SWT.DROP_DOWN);

FMenu.SetMenu (filemenu);

Among them, the first two lines have added a STYLEM for MenuBar for MenuItem. The third line is to create a new menu, his style is Drop_Down, which is represented as a down menu. Then, the fourth line is to associate the menu item and this menu, which is similar to the context menu mentioned, ie, regards FileMenu as the pop-up menu of FMenu.

4. If you need to create a pop-up menu, we need to create a new style for POP_UP, his role is similar to the menbar mentioned in 2, and subsequent work is based on this new Pop_up menu.

5. Menuitem can use Style has: Push, Check, Radio, Separetor, Cascade.

IV. structural diagram:

Shell

Menu

Menuitem

e) Tree:

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test5 {

PRIVATE TREE Tree = NULL;

...

Private vidinit () {

...

Tree = New Tree (s, swt.single | swt.border | swt.check);

Tree.setsize (300, 300);

TreeItem Tic = New TreeItem (Tree, Swt.none);

TiC.SetText ("C");

TreeItem Tid = New TreeItem (Tree, Swt.none);

Tid.Settext ("D");

TreeItem Tipro = New TreeItem (TIC, SWT.NONE); TipRo.Settext ("Program file");

TreeItem Tijava = New TreeItem (TID, SWT.NONE);

Tijava.Settext ("Java");

Tree.AddSelectionListener (New SelectionAdapter () {

@Override public void widgetSelected (SelectionEvent E) {

TreeItem [] TIS = Tree.getSelection ();

For (TreeItem Ti: TIS) {

System.out.println (Ti.getText () "was success"

Ti.GetChecked ());

}

}

});

...

}

...

}

Ii. Running:

Iii. Description:

1. STYLE you can use with: Multi, Single, and Check. Where Multi and Single represent radio and multi-selection; Check is to add a check box for each tree node.

IV. structural diagram:

Shell

Tree

TreeItem

f) Toolbar:

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test6 {

...

Private vidinit () {

...

Toolbar TB = New Toolbar (S, SWT.BORDER | SWT.HORIZONTAL);

ToolItem Tiopen = New ToolItem (TB, SWT.PUSH);

Tiopen.Settext ("open");

ToolItem TiClose = New ToolItem (TB, SWT.PUSH);

Ticlose.setText ("close");

Group g = new group (s, swt.border);

G.Settext ("group");

...

}

...

}

Ii. Running:

Iii. Description:

IV. structural diagram:

Shell

Toolbar

ToolItem

g) Coolbar:

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

PUBLIC Class test7 {

...

Private vidinit () {

...

Coolbar CB = New Coolbar (S, SWT.BORDER);

Cb.setLayout (new rowlayout ());

COOLITEM CI1 = New COOLITEM (CB, SWT.BORDER);

COOLITEM CI2 = New COOLITEM (CB, SWT.BORDER);

Button B = New Button (CB, SWT.PUSH);

B.SetText ("Button");

Toolbar TB = New Toolbar (CB, SWT.BORDER | SWT.HORIZONTAL); TOOLITEM TIOPEN = New ToolItem (TB, SWT.PUSH);

Tiopen.Settext ("open");

ToolItem TiClose = New ToolItem (TB, SWT.CHECK);

Ticlose.setText ("close");

Ci1.setControl (b);

Ci2.setControl (TB);

...

}

...

}

Ii. Running:

Iii. Description:

IV. structural diagram:

CONTROL

Shell

Coolbar

CoolItem

CONTROL

h) message box

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test9 {

...

Private vidinit () {

...

MessageBox box = new messagebox (s, swt.icon_question | swt.yes | swt.no);

Box.SetMessage ("Are you a student?");

Box.Settext ("Question");

Int res = Box.Open ();

Switch (res) {

Case Swt.yes: system.out.println ("Select Yes");

Break;

Case Swt.no:system.out.println ("Select NO ");

Break;

}

ColorDialog Cd = New ColorDialog (s);

CD.SETTEXT ("SELECTED Color");

Cd.setrGB (New RGB (255, 0, 0));

RGB RGB = cd.open ();

System.out.println ("SELECT Color IS" RGB);

DirectoryDialog DD = New DirectoryDialog (s);

DD.SETTEXT ("SELECTED DIR");

DD.SETFILTERPATH ("D: /");

String path = dd.open ();

System.out.println ("SELECT PATH IS" PATH);

FileDialog fd = New FileDialog (s, swt.open);

fd.setText ("SELECT FILE");

fd.setfilterpath ("D: /");

fd.setFileName ("*. java");

String file = fd.open ();

System.out.println ("SELECT PATH IS" File;

FONTDIALOG FOD = New FontDialog (s);

FontData Fontd = New Fontdata ("New Song", 9, SWT.NONE);

FOD.SETTEXT ("SELECT FONT");

FOD.SETFONTLIST (New FontData [] {fontd});

FOD.SETRGB (New RGB (0, 0, 0)); fontdata font = FOD.OPEN ();

System.out.println ("SELECT FONT IS" FONT);

PRINTDIALOG PD = New PrintDialog (s);

PrinterData PDATA = pd.open ();

System.out.println ("Print Data IS" PDATA);

...

}

...

}

Ii. Running:

Messagebox

Colordialog

DirectoryDialog

FileDialog

FontDialog

PrintDialog

Iii. Description:

IV. structural diagram:

6. Drawing:

a) code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test8 {

Private Canvas Canvas = NULL;

...

Private vidinit () {

...

Canvas = New Canvas (S, SWT.BORDER);

Canvas.setsize (200, 200);

S.PACK ();

S.Open ();

GC graphic = New GC (Canvas);

Paint (graphic, d);

graphic.dispose ();

Canvas.addpainTlistener (New PainTlistener () {

Public void PaintControl (Paintevent E) {

Paint (E.GC, E.Display);

}

});

...

}

Private void Paint (GC G, Display D) {

Color red = d.getsystemColor (SWT.COLOR_RED);

Font f = New font (D, "New Song", 9, SWT.NONE);

Image i = new image (d, "11.jpg");

G.setForeground (RED);

g.setfont (f);

g.drawRectangle (0, 0, 80, 80);

g.drawtext ("Hello World", 20, 20);

G.drawImage (i, 0, 0, i.getbounds (). Width, I.GetBounds (). HEIGHT, 30, 40, 40,

40);

i.dispose ();

f.dispose ();

}

...

}

b) Runchart:

c) Description:

i. SWT provides something called GRAPHICCONTEXT for drawing, each control can belong to his own GC.

II. In SWT, font, color, and pictures are weight objects, so after use, explicit DISPOSE () operations should be performed.

Due to the way, we cannot overload the drawing method, so we need to over-draw the image through the event processing.

7. Layout

a) FillLayout

i. Code: / **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test1 {

...

Private vidinit () {

Display d = new display ();

Shell s = new shell ();

S.Settext ("FillLayout");

S.setsize (200, 200);

S.setLayout (New FillLayout ());

Button [] BS = New Button [4];

For (int i = 0; i

BS [I] = New Button (S, SWT.PUSH | SWT.BORDER);

BS [i] .SETTEXT ("Button i);

}

S.PACK ();

S.Open ();

While (! s.isdisposed ()) {

IF (! D.ReadandDispatch ()) {

D.sleep ();

}

}

D. Dispose ();

}

Public static void main (String [] args) {

TEST1 T = New Test1 ();

}

}

Ii. Running:

Iii. Description:

1. The role of FillLayout is similar to the flowLayout in swing.

b) RowLayout

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test2 {

...

Private vidinit () {

...

RowLayout Layout = New RowLayout ();

Layout.wrap = True;

S.setLayout (layout);

Button [] BS = New Button [4];

For (int i = 0; i

BS [I] = New Button (S, SWT.PUSH | SWT.BORDER);

BS [i] .SETTEXT ("Button i);

}

...

}

...

}

Ii. Running:

Iii. Description:

1. RowLayout passes the WRAP attribute, so that when one line cannot accommodate all controls, automatically move the control to the next line.

c) GridLayout

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test3 {

...

Private vidinit () {

...

GridLayout Layout = New GridLayout ();

Layout.numcolumns = 2;

S.setLayout (layout);

Button [] BS = New Button [4];

For (int i = 0; i

BS [I] = New Button (S, SWT.PUSH | SWT.BORDER);

BS [i] .SETTEXT ("Button i);

...

}

...

}

Ii. Running:

Iii. Description:

1. The use of GridLayout is similar to GridLayout in Swing.

d) formlayout

i. Description: Too complicated, no matter.

8. Other fun functions

a) Use the system tray (TRAY)

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test1 {

Private menu mpop = NULL;

...

Private vidinit () {

Display d = new display ();

Shell s = new shell ();

TRAY TRAY = d.getsystemtray ();

TRAYITEM TI = New TrayITEM (TRAY, SWT.NONE);

Ti.SetTooltiptext ("My TRAY");

Image i = new image (d, "11.jpg");

Ti.setImage (i);

i.dispose ();

MPOP = New Menu (S, SWT.POP_UP);

Menuitem Miopen = New Menuitem (MPOP, SWT.NONE);

IOPEN.SETTEXT ("Open");

Ti.Addlistener (SWT.MENUDETECT, New Listener () {

Public void Handleevent (Event E) {

MPOP.SetVisible (TRUE);

}

});

Ti.AddSelectionListener (New SelectionAdapter () {

@Override public void widgetSelected (SelectionEvent E) {

System.out.println ("SELECTED My TRAY");

}

});

...

}

...

}

Ii. Running:

Iii. Description:

b) browser

i. Code:

/ **

* @Author cenyongh@mails.gscas.ac.cn

* /

Public class test4 {

Private browser b = NULL;

PRIVATE TEXT T = NULL;

...

Private vidinit () {

...

Composite C = New Composite (S, SWT.NONE);

C.SetLayout (New GridLayout (2, False);

Griddata data2 = new griddata ();

Data2.WidthHint = 60;

Label L = New Label (C, SWT.NONE);

L.SETTEXT ("Address:");

L.setLayOutdata (DATA2);

Data2 = New GridData (GridData.Fill_horizontal);

T = new text (c, swt.border);

T.SetLayOutdata (DATA2); GridData Data = New GridData (GridData.Fill_horizontal);

C.setLayOutdata (data);

Data = New GridData (GridData.Fill_both);

B = New Browser (s, swt.border);

B.setLayOutdata (data);

T.AddKeylistener (new keyadapter () {

@Override public void keypressed (KeyEvent E) {

IF (E.CHARACTER == SWT.CR) {

String url = t.getText ();

System.out.println (URL);

B.SETURL (URL);

}

}

});

...

}

...

}

Ii. Running:

Iii. Description:

9. Reference:

a) http://dev.eclipse.org/ViewCVS/index.cgi/~c-swout~/platform-swt-home/dev.html

b) http://www.cs.umanitoba.ca/~eclipse/

转载请注明原文地址:https://www.9cbs.com/read-63209.html

New Post(0)