Home > Database > Mysql Tutorial > body text

开发基于Java的图形用户界面_MySQL

WBOY
Release: 2016-06-01 14:06:34
Original
1304 people have browsed it

  SWT(Standard Widget Toolkit)是IBM推出的“基于java”的图形界面开发库,我之所以说它是“基于java”的意思是程序员编写代码的时候是使用java语言,事实上SWT的底层实现是C语言完成的。但是这些对程序员是透明的。

  我们使用SWT开发GUI程序的时候,直接用SWT API来写。事实上很多java的代码是通过JNI去掉用C代码来实现的。针对不同的平台每个类有不同的实现方式,这篇文章的目的不在于讲述SWT的设计原理,如果你对这些感兴趣的话可以参考http://www.eclipse.org/articles/Article-SWT-Design-1/SWT-Design-1.html。

  下面开始介绍如何使用SWT,首先我假设你已经安装了Eclipse3.0,当然其他的版本也可以,如果还没有的话从www.eclipse.org上去下载。

  运行Eclipse,切换到java透视图下面,从Package explore里面新建一个java project。名称为Test。

  在Libraries里面选择Add external JARs来添加运行SWT所需要的类库,这个和系统相关的,比如我在windows xp下,地址为:D:eclipsepluginsorg.eclipse.swt.win32_3.0.0wswin32swt.jar。把它添加进来,建议做一个variables指向这个swt.jar文件,以后直接添加variables就可以了。

  编写java代码,例如

  import org.eclipse.swt.widgets.*;
  import org.eclipse.swt.*;
  public class SWTHello {
  public static void main(String[] args) {
  Display display = new Display();
  Shell shell = new Shell(display);
  Label label = new Label(shell, SWT.NONE);
  label.setText("Hello, World!");
  shell.pack();
  label.pack();
  shell.open();
  while(!shell.isDisposed())
   if(!display.readAndDispatch())
    display.sleep();
    display.dispose();
    label.dispose();
  }
  }

  配置运行环境,由于SWT程序运行的时候要用到本机资源,如果你现在运行上面的程序的时候会出现错误,类似于java.lang.UnsatisfiedLinkError: no swt-win32-2133 in java.library.path”,因此你必须指定所需的DLL的位置,具体做法是:从菜单run->run切换到运行配置界面,选择Arguments在VM Arguments里面写入-Djava.library.path=例如在我的机器上DLL的地址为D:eclipsepluginsorg.eclipse.swt.win32_3.0.0oswin32†。这样每次运行都要配置显得有点麻烦,所以建议你在环境变量PATH里面添加这个包括DLL的路径。

  运行程序,你会看到你的第一程序的效果 :) 我的感觉是挺不错的,速度要比Swing/AWT快。

  关于如何使用SWT,请参考www.eclipse.org上面的相关文章,多看看AWT API,多写一些代码!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!