探索Eclipse的嵌入式富客户端平台:移动设备需要Eclipse(3)
时间:2011-11-11 IBM Chris Aniszczyk
eSWT 核心的示例
我们从一个完整的 eSWT 程序开始,这个程序创建 一个窗口,在应用程序的标题栏上显示 “HelloWorld”。然后我们将添加一些核心控件(文 本、按钮、列表)。对于布局,我们采用 FormLayout 和 FormData。还将在按钮上添加 SelectionListener,它将弹出一个消息框。完整的代码如清单 1 所示。
清单 1:eSWAT 风格的 HelloWorld
01 public class HelloWorldeSWT {
02
03 public static void main(String[] args) {
04 Display display = new Display();
05 final Shell shell = new Shell (display);
06
07 Text text = new Text(shell,SWT.SINGLE);
08 text.setText("This is a text");
09 Button buttonleft = new Button(shell, SWT.PUSH);
10 buttonleft.setText("Left Push!");
11 Button buttonright = new Button(shell, SWT.PUSH);
12 buttonright.setText("Right Push!");
13 buttonright.addSelectionListener(new SelectionListener(){
14 public void widgetSelected(SelectionEvent e) {
15 MessageBox messageBox = new MessageBox (shell,
16 SWT.ICON_INFORMATION| SWT.YES | SWT.NO);
17 messageBox.setText("MessageBox");
18 messageBox.setMessage("Can you see me?");
19 messageBox.open();
20 }
21 public void widgetDefaultSelected(SelectionEvent e) {
22 }});
23 List list = new List (shell,SWT.MULTI|SWT.BORDER);
24 for (int i=0; i<5; i++) {
25 list.add("item "+i);
26 }
27
28 shell.setText("HelloWorld from eSWT");
29 FormLayout layout = new FormLayout();
30 layout.spacing = 5;
31 layout.marginHeight = layout.marginWidth = 9;
32 shell.setLayout(layout);
33
34 FormData textData = new FormData();
35 textData.top = new FormAttachment (0);
36 textData.left = new FormAttachment(0);
37 textData.right = new FormAttachment(90);
38 text.setLayoutData(textData);
39
40 FormData buttonleftData = new FormData();
41 buttonleftData.top = new FormAttachment (text);
42 buttonleftData.left = new FormAttachment(0);
43 buttonleftData.right = new FormAttachment(40);
44 buttonleft.setLayoutData (buttonleftData);
45
46 FormData buttonrightData = new FormData();
47 buttonrightData.top = new FormAttachment(text);
48 buttonrightData.left = new FormAttachment(buttonleft);
49 buttonright.setLayoutData(buttonrightData);
50 FormData listData = new FormData();
51
52 listData.top = new FormAttachment (buttonleft);
53 list.setLayoutData(listData);
54
55 shell.setSize (240,320);
56 shell.open();
57
58 while (!shell.isDisposed()) {
59 if (!display.readAndDispatch())
60 display.sleep();
61 }
62 display.dispose();
63 }
探索Eclipse的嵌入式富客户端平台:移动设备需要Eclipse(4)
时间:2011-11-11 IBM Chris Aniszczyk
图 2 显示了在日文版 PocketPC 设备上运行的应用程序。当用户按下 Right Push |