SWT/JFace中实现文件阅读器

1.新建一个"swt/jface"项目
Image00000.jpg
2.新建designer application Window文件
Image00001.jpg
3.在可视界面加入MenuBar
Image00002.jpg
4.再加入一个SubMenu名为File
Image00003.jpg
5.分别加入两个Menuitem 取名分别为OPen 和Quit
Image00004.jpg
6.导入一个文本Text,并且Lines为Multi
Image00005.jpg
7.分别双击OPen 与Quit ,这样就注册了事件处理方法"WidgetSelected"的SelectionListener监听器.出现如下代码.
Image00006.jpg
Image00007.jpg
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.运行结果如下:
Image00008.jpg
Image00009.jpg

你可能感兴趣的:(jface)