`
包子_feiFEI
  • 浏览: 70834 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类

SWT备忘

 
阅读更多

1.SWT

SWT是由IBM所提出的,一个用于做用户界面的包。她与SunSwing具有很多的相似之处,因此如果掌握了Swing,那么要学习SWT将是非常简单的。SWTEclipse中得到了很大的使用,但是,在Eclipse当中更多情况下,我们使用的是对SWT再进行了一层封装的JFACE包。因此,如果想进行Eclipse的插件开发,我们需要先掌握SWTJFACE

2.从例子开始

a)代码:

/**

* @authorcenyongh@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)运行图:

b)说明:

从上述程序,我们可以大约的得到一些SWTSwing不一样的地方:

1.当我们需要实例化一个控件时,我们需要在构造函数中提供父控件的引用。

2.通过构造函数,我们不需要进行显式的add()操作,来组织控件树。

3.我们需要手动的启动消息循环(while部分)。

4.程序结束的时候,我们需要手工的对资源进行释放。

3.控件:

a)Label

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test1 {

public Test1() {

init();

}

private void init() {

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.运行图:

iii.说明:

1.Label实例化的时候需要提供一个Style参数,从后面的例子可以看到,这个Style参数在SWT当中被广泛使用,通过Style参数的使用,我们可以比在Swing当中接触到更少的子类。

2.Label可以使用的Style参数有:BORDERCENTERLEFTRIGHTWRAPSEPARATORBORDER用于为控件添加边框;CENTERLEFTRIGHT用于Label内容的对齐;WRAP用于当文本内容超出Label一行的最大长度时,进行自动换行。当使用SEPARATOR时,可以配合HORIZONTALVERTICALSHADOW_INSHADOW_OUTSHADOW_NONE使用。

b)Text

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test2 {

public Test2() {

init();

}

private void init() {

Text t1 = new Text(s, SWT.NONE);

/**

*相当于密码输入框

*/

Text t2 = new Text(s, SWT.BORDER);

t2.setEchoChar('*');

t2.setTextLimit(10); //限制最大输入字符数为10

/**

*不能换行,相当于TextField

*/

Text t3 = new Text(s, SWT.SINGLE);

t3.setText("Hello World");

/**

*支持换行

*/

Text t4 = new Text(s, SWT.MULTI);

t4.setText("Hello World");

/**

*相当于TextArea

*/

Text t5 = new Text(s, SWT.H_SCROLL | SWT.V_SCROLL);

/**

*支持自动换行

*/

Text t6 = new Text(s, SWT.WRAP);

}

public static void main(String[] args) {

Test2 t = new Test2();

}

}

ii.运行图:

iii.说明:

1.Text可以使用的Style参数有:BORDERH_SCROLLV_SCROLLMULTISINGLEREAD_ONLYWRAPBORDER用于添加边框;H_SCROLLV_SCROLL用于提供滚动,当使用滚动时即表明Text可以添加多行内容;MULTISINGLE用于表示Text的内容时多行还是单行;READ_ONLY用于表示Text为止读。WRAP用于当文本内容超出Text一行的长度时,进行自动换行。

c)Button

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test3 {

public Test3() {

init();

}

private void init() {

/**

*普通按钮

*/

Button b1 = new Button(s, SWT.PUSH | SWT.LEFT);

b1.setText("Button 1");

/**

*单选按钮

*/

Button b2 = new Button(s, SWT.CHECK);

b2.setText("Button 2");

/**

* Radio按钮

*/

Button b3 = new Button(s, SWT.RADIO);

b3.setText("Button 3");

/**

*箭头按钮

*/

Button b4 = new Button(s, SWT.ARROW | SWT.BORDER);

b4.setText("Button 4");

/**

* Toggle按钮

*/

Button b5 = new Button(s, SWT.TOGGLE);

b5.setText("Button 5");

}

}

ii.运行图:

iii.说明:

1.Button可以使用的Style有:PUSHCHECKRADIOTOGGLEARROWFLAT, BORDER, LEFT, RIGHTCENTER。其中,前5个用于改变按钮的类别;后5个用于改变按钮的外观。

2.由于SWT没有提供类似于Swing当中的ButtonGroup的概念,因此,我们需要以手工的方式来处理按钮间的互斥选择问题。

d)List

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test4 {

private void init() {

/**

*多选

*/

List l1 = new List(s, SWT.MULTI | SWT.H_SCROLL | SWT.BORDER);

l1.add("Item 1");

l1.add("Item 2");

l1.add("Item 3");

/**

*单选

*/

List l2 = new List(s, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);

l2.setItems(new String[] { "Item 1", "Item 2", "Item 3" });

}

}

ii.运行图:

iii.说明:

1.List可以使用的Style有:BORDERH_SCROLLV_SCROLLSINGLEMULTISINGLEMULTI用于表示单选和多选。

e)Combo

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test5 {

private Combo c2 = null;

public Test5() {

init();

}

private void init() {

/**

*没有编辑框

*/

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" });

/**

*没有选项

*/

Combo c3 = new Combo(s, SWT.DROP_DOWN);

}

}

ii.运行图:

iii.说明:

1.Combo可以使用的Style有:BORDERDROP_DOWNREAD_ONLYSIMPLEDROP_DOWN表示只提供下拉框;SIMPLE表示既有下拉框又有编辑框。

f)Composite

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test6 {

private void init() {

/**

* Composite就是一个容器,他类似于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.运行图:

iii.说明:

1.Composite是一个容器,相当于Swing当中的JPanel

2.Composite可以使用的Style有:BORDERH_SCROLLV_SCROLL

g)Group

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test7 {

private void init() {

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.运行图:

iii.说明:

1.GroupComposite的一个子类,所以他也是一个容器,不过它与Composite的区别就是,她比Composite多加了一个框。

2.Group可以使用的Style有:BORDERSHADOW_ETCHED_INSHADOW_ETCHED_OUTSHADOW_INSHADOW_OUTSHADOW_NONE

4.事件监听

a)SelectionListener

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test1 {

private void init() {

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.说明:

1.SelectionListener的作用与Swing当中的ActionListener同。

b)KeyListener

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test2 {

private void init() {

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");

//通过doit属性,可以禁止输入的字符

e.doit = false;

} else {

System.out.println("Pressed " + e.character);

}

}

});

}

}

ii.说明:

1.SWT没有为按键提供与Swing中类似的平台无关的VK码。

2.我们可以通过KeyEvent中的doit属性,来对输入框的内容进行约束。这是比Swing要方便的地方,如果我们想要在Swing当中完成相同的操作的话,可能需要获取TextField中对应的module对象,并进行操作。

c)MouseListenerMouseMoveListenerMouseTrackListener

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test3 {

private void init() {

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.说明:

1.SWT增加了MouseTrackerListener,用于监听鼠标进入,离开和在控件上停留的事件。

d)FocusListener

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test5 {

private void init() {

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 keyTraversed(TraverseEvent e) {

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

System.out.println("Don't use Tab to Traverse.");

e.doit = false;

}

}

});

i++;

}

}

}

ii.说明:

1.FocusListener在控件获取到焦点之后得到通知。

2.TraverseListener在控件获得焦点之前得到通知,通过TraverseEventdetail值可以得知控件是通过什么方式获得焦点,而通过doit值,可以限制是否让控件获取该焦点。

5.高级控件

a)Table

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test1 {

private Table table = null;

private void init() {

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.运行图:

iii.说明:

1.Table可以使用的Style值:BORDERH_SCROLLV_SCROLLSINGLEMULTICHECKFULL_SELECTIONHIDE_SELECTION。其中SINGLEMULTI用于支持单选和多选;CHECK用于为第一列增加一个CheckBoxFULL_SELECTION用于当选择某一列时,可以把该项所在的行全部选中。HIDE_SELECTION用于禁止表格的项被选中。

iv.结构图:

Shell

Table

TableColumn

TableItem

b)TabFolder

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test2 {

private void init() {

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.NONE);

page2.setText("Page 2");

page2.setControl(c2);

}

}

ii.运行图:

iii.说明:

1.Tab可以使用的Style有:BORDER

iv.结构图:

control

Shell

TabFolder

TabItem

Control

c)SliderScaleProgressBar

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test3 {

private Slider slider = null;

private Scale scale = null;

private ProgressBar pb = null;

private void init() {

slider = new Slider(s, SWT.HORIZONTAL);

slider.setMinimum(0);

slider.setMaximum(100);

slider.addSelectionListener(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.运行图:

iii.说明:

1.SliderScaleProgressBar可以使用的StyleBORDERHORIZONTALVERTICAL

d)菜单:

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test4 {

private void init() {

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");

/**

*弹出式菜单

* */

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);

pmiCopy.setText("Copy");

b.setMenu(mPopup);

}

}

ii.运行图:

iii.说明:

1.与菜单相关的类只有MenuMenuItem。而为了组成整个菜单栏,我们需要通过不同的Style来标识不同的控件。

2.MenuStyleBar时,表示这个Menu是个MenuBar

3.如果需要新建一个菜单,我们需要:

MenuItem fMenu = new MenuItem(bar, SWT.CASCADE);

fMenu.setText("File");

Menu fileMenu = new Menu(s, SWT.DROP_DOWN);

fMenu.setMenu(fileMenu);

其中,前两行是为MenuBar添加了一个StyleCASCADEMenuItem。第三行是新建一个菜单,他的StyleDROP_DOWN,表示为一个下来菜单。然后第4行,是把菜单项和这个菜单关联起来,这于待会提到的上下文菜单类似,即把fileMenu看作是fMenu的弹出菜单。

4.如果需要建立一个弹出式菜单,我们需要新建一个StylePOP_UP的菜单,他的作用与2中提到的MenuBar类似,后续的工作都是基于这个新建的POP_UP菜单继续的。

5.MenuItem可以使用的Style有:PUSHCHECKRADIOSEPARATORCASCADE

iv.结构图:

Shell

Menu

MenuItem

e)树:

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test5 {

private Tree tree = null;

private void init() {

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 selected "

+ ti.getChecked());

}

}

});

}

}

ii.运行图:

iii.说明:

1.Tree可以使用的Style有:MULTISINGLECHECK。其中MULTISINGLE表示单选和多选;CHECK是为每个树节点添加复选框。

iv.结构图:

Shell

Tree

TreeItem

f)ToolBar

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test6 {

private void init() {

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.运行图:

iii.说明:

iv.结构图:

Shell

ToolBar

ToolItem

g)CoolBar

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test7 {

private void init() {

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.运行图:

iii.说明:

iv.结构图:

control

Shell

CoolBar

CoolItem

Control

h)消息框

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test9 {

private void init() {

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("新宋体", 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.运行图:

MessageBox

ColorDialog

DirectoryDialog

FileDialog

FontDialog

PrintDialog

iii.说明:

iv.结构图:

6.绘图:

a)代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test8 {

private Canvas canvas = null;

private void init() {

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, "新宋体", 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)运行图:

c)说明:

i.SWT提供一个叫做GCGraphicContext)的东西用于绘图,每个控件都可以属于他自己的GC

ii.SWT中,字体,颜色,图片都是重量对象,因此使用完以后,应该进行显式的dispose()操作。

iii.由于,我们不能像Swing当中那样,通过子类化的方法,对绘图方法进行重载,因此我们需要通过事件处理的方式进行图像的重绘。

7.布局

a)FillLayout

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test1 {

private void init() {

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.length; 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.运行图:

iii.说明:

1.FillLayout的作用与Swing当中的FlowLayout相似。

b)RowLayout

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test2 {

private void init() {

RowLayout layout = new RowLayout();

layout.wrap = true;

s.setLayout(layout);

Button[] bs = new Button[4];

for (int i = 0; i < bs.length; i++) {

bs[i] = new Button(s, SWT.PUSH | SWT.BORDER);

bs[i].setText("Button " + i);

}

}

}

ii.运行图:

iii.说明:

1.RowLayout通过wrap属性,可以使得当一行不能容下所有控件时,自动把控件下移到下一行。

c)GridLayout

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test3 {

private void init() {

GridLayout layout = new GridLayout();

layout.numColumns = 2;

s.setLayout(layout);

Button[] bs = new Button[4];

for (int i = 0; i < bs.length; i++) {

bs[i] = new Button(s, SWT.PUSH | SWT.BORDER);

bs[i].setText("Button " + i);

}

}

}

ii.运行图:

iii.说明:

1.GridLayout的使用与Swing中的GridLayout类似。

d)FormLayout

i.说明:太复杂了,不管。

8.其他有趣功能

a)使用系统托盘(Tray

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test1 {

private Menu mPop = null;

private void init() {

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.运行图:

iii.说明:

b)浏览器

i.代码:

/**

* @authorcenyongh@mails.gscas.ac.cn

*/

public class Test4 {

private Browser b = null;

private Text t = null;

private void init() {

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.运行图:

iii.说明:

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics