SWT JFACE 开发配制

eclipse 3.6中开发RCP:
  要用到SWT/JFace 但在测试SWT时遇到了问题:
需要的库文件有哪些呢?打开eclipse安装目录下的plugins文件夹,我们需要找到以下jar文件:
l         org.eclipse.swt_3.x.x.jar
l         org.eclipse.jface_3.x.x.jar
l         org.eclipse.core.runtime_3.x.x.jar
l         org.eclipse.ui.workbench_3.x.x.jar
 打开工程的properties对话框,然后选择Java Build Path中的Libraries选项卡,将这些jar导入进来
第三步:为你的java程序添加本地库文件。如果你使用windows的话,你可能注意到在eclipse的plugins目录下还有一个org.eclipse.swt.win32_3.x.x.jar,将这个jar解压以后在os/win32/x86目录下有几个dll文件。这几个dll为swt通过JNI访问windows本地API提供了接口,我们需要将使java程序在启动时候即能够访问它。你可以有多种办法实现这个目的:
最简单的办法就是直接把这几个文件拷贝到你jdk的bin目录下
第一道门槛 : java.lang.NoClassDefFoundError: org/eclipse/core/runtime/IProgressMonitor
 
很多人说导入org.eclipse.core.runtime_x.x.x.jar 就ok,但你自己试试就知道并不是所有版本的org.eclipse.core.runtime_x.x.x.jar 里都有IProgressMonitor这个class.

所以即使你导入了org.eclipse.core.runtime_x.x.x.jar 到project也可能无济于事。

去plugins下面找找org.eclipse.equinox.common.x.x.x.jar,然后导入到project ,你会发现IProgressMonitor 悠然的躺在那里。

 

第二道门槛 :
 Exception in thread "main" java.lang.UnsatisfiedLinkError: no swt-win32-3139 in java.library.path

 因为SWT使用了JNI调用C,所以你需要把相对应版本的dll文件(swt-win32-xxxx.dll)copy到C:/windows/system32下面.

 还可以把5个DLL放到JAVA\BIN下面
第三道门槛 : Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/core/commands/common/EventManager

     解决了上面两个问题以为大功告成,其实还差一步。导入org.eclipse.core.commandsx.x.x.jar到project中,我们的第一个SWT/JFace就能跑起来了。

 

要导入的JAR:


D:\J2EE\eclipse-rcp-3.6\eclipse\plugins\org.eclipse.swt.win32.win32.x86_3.6.0.v3650b.jar
D:\J2EE\eclipse-rcp-3.6\eclipse\plugins\org.eclipse.jface_3.6.0.I20100601-0800.jar
D:\J2EE\eclipse-rcp-3.6\eclipse\plugins\org.eclipse.core.runtime_3.6.0.v20100505.jar
D:\J2EE\eclipse-rcp-3.6\eclipse\plugins\org.eclipse.ui.workbench_3.6.0.I20100603-1100.jar
D:\J2EE\eclipse-rcp-3.6\eclipse\plugins\org.eclipse.equinox.common_3.6.0.v20100503.jar
D:\J2EE\eclipse-rcp-3.6\eclipse\plugins\org.eclipse.equinox.event_1.2.0.v20100503.jar
D:\J2EE\eclipse-rcp-3.6\eclipse\plugins\org.eclipse.core.commands_3.6.0.I20100512-1500.jar

代码:

 

package com.swtjface.test;

import org.eclipse.jface.window.*;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;

public class HelloSWTJFace extends ApplicationWindow {
 public static void main(String[] args) {
  HelloSWTJFace awin = new HelloSWTJFace();
  awin.setBlockOnOpen(true);
  awin.open();
  Display.getCurrent().dispose();
 }

 public HelloSWTJFace() {
  super(null);
 }

 protected Control createContents(Composite parent) {
  Text helloText = new Text(parent, SWT.CENTER);

  helloText.setText("Hello SWT and JFace!");
  parent.pack();
  return parent;
 }

}

 

 

 

package com.swtjface.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class HelloSwt {
 public static void main(String[] args) {
  Display display = new Display();
  Shell shell = new Shell(display);
  Text helloText = new Text(shell, SWT.NONE);
  helloText.setText("Hello SWT!");
  helloText.pack();

  shell.pack();
  shell.open();
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }
  display.dispose();
 }
}


 

 

posted @ 2010-08-13 12:47  夜色狼  阅读(698)  评论(0编辑  收藏  举报