1.新建一个"swt/jface"项目

2.新建designer application Window文件

3.在可视界面加入MenuBar

4.再加入一个SubMenu名为File

5.分别加入两个Menuitem 取名分别为OPen 和Quit

6.导入一个文本Text,并且Lines为Multi

7.分别双击OPen 与Quit ,这样就注册了事件处理方法"WidgetSelected"的SelectionListener监听器.出现如下代码.


8.分别加入代码:
openMenuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
FileDialog fd = new FileDialog(shell, SWT.OPEN);
String s = fd.open();
if (s != null && !s.equals("")) {
FileReader fr;
try {
fr = new FileReader(s);
BufferedReader br = new BufferedReader(fr);
String s1 = br.readLine();
while (s1 != null) {
text.append(s1 + "\n");
s1 = br.readLine();
}
br.close();
fr.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
});
quitMenuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
shell.close();
}
});
9.运行结果如下:


posted on 2006-08-25 12:21
摩西 阅读(305)
评论(0) 编辑 收藏