科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道技术新知:基于Eclipse 3.0的SWT编程

技术新知:基于Eclipse 3.0的SWT编程

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

  一、SWT简介      Java语言的声望和它在桌面应用程序(GUI程序)所取得的成就显然极不相符,至今仍然很少能看到非常成功Java桌面程序。虽然有JBuilder,Netbean,JProbe等大型软件作为代表。

作者:中国IT实验室 来源:中国IT实验室 2007年9月23日

关键字: Eclipse 编程 java

  • 评论
  • 分享微博
  • 分享邮件

  一、SWT简介
  
  Java语言的声望和它在桌面应用程序(GUI程序)所取得的成就显然极不相符,至今仍然很少能看到非常成功Java桌面程序。虽然有JBuilder,Netbean,JProbe等大型软件作为代表,但这仍不能证明Java的GUI程序是成功的:它们的外观总是和同一操作系统平台下的其它软件显得格格不入。对机器配置的需求也似乎永无止境,这使得它们只能被一些总是拥有当前最高性能PC的程序员们所容忍,或是那些不在乎金钱和时间的专业用户所接受。对绝大多数计算机使用者来说,AWT或SWING代表着怪异的界面和无法接受的速度。Standard Widget Toolkit(SWT)或许是Java这一噩梦的终结者,广大Java程序员终于可以开发出高效率的GUI程序,它们拥有标准的外观,几乎没有人能看出你的程序是用Java写出来的,更为重要的是,这些程序是跨平台的。
  
  SWT本身仅仅是Eclipse组织为了开发Eclipse IDE环境所编写的一组底层图形界面 API。或许是无心插柳,或是有意为之,至今为止,SWT无论是在性能和外观上,都超越了SUN公司提供的AWT和SWING。目前Eclipse IDE已经开发到了2.1版本,SWT已经十分稳定。这里指的稳定应该包含两层意思:
  
  一是指性能上的稳定,其中的关键是源于SWT的设计理念。SWT最大化了操作系统的图形构件API,就是说只要操作系统提供了相应图形的构件,那么SWT只是简单应用JNI技术调用它们,只有那些操作系统中不提供的构件,SWT才自己去做一个模拟的实现。可以看出SWT的性能上的稳定大多时候取决于相应操作系统图形构件的稳定性。
  
  另一个稳定是指SWT API包中的类、方法的名称和结构已经少有改变,程序员不用担心由于Eclipse组织开发进度很快(Eclipse IDE每天都会有一个Nightly版本的发布),而导致自己的程序代码变化过大。从一个版本的SWT更新至另一版本,通常只需要简单将SWT包换掉就可以了。
  
  二、Eclipse3.0的SWT编程
  
  1.SWT比AWT和Swing要快多,因为它是利用操作系统的界面组件生成UI的,在java桌面设计领域掀起一场革命
  
  2.环境配置:
  
  windows系统+eclipse3.0
  
  3.具体操作:
  
  (1).新建一java项目,命名SWT,文件结构如下:
  
  +swt
  
  +bin(编译输出)
  
  +src(原文件)
  
  +AddressBookUI.java
  
  +swt-awt-win32-3062.dll(以下均从eclipse\plugins\org.eclipse.swt.win32_3.0.1\os\win32\x86下导入)
  
  +swt-win32-3062.dll
  
  +javaw.exe.manifest
  
  (2).到项目的properties里,在java build path | libraries里将swt.jar导入
  
  (3).AddressBookUI.java原代码如下:
  
  import org.eclipse.swt.widgets.Display;
  
  import org.eclipse.swt.widgets.Shell;
  
  import org.eclipse.swt.SWT;
  
  import org.eclipse.swt.widgets.Button;
  
  import org.eclipse.swt.widgets.Group;
  
  import org.eclipse.swt.widgets.Label;
  
  import org.eclipse.swt.widgets.Text;
  
  import org.eclipse.swt.widgets.*;
  
  import org.eclipse.swt.events.SelectionAdapter;
  
  import org.eclipse.swt.events.SelectionEvent;
  
  public class AddressBookUI {
  
  private Shell shell;
  
  private Text miscText;
  
  private Text addrText;
  
  private Text emailText;
  
  private Text phoneText;
  
  private Text lnameText;
  
  private Text fnameText;
  
  private Button cancelButton;
  
  private Button saveButton;
  
  private Button nextButton;
  
  private Button prevButton;
  
  private Button editButton;
  
  private Button deleteButton;
  
  private Button newButton;
  
  public static void main(String[] args) {
  
  AddressBookUI window = new AddressBookUI();
  
  window.open();
  
  }
  
  public void open() {
  
  final Display display = new Display();
  
  shell = new Shell();
  
  shell.setSize(610, 477);
  
  shell.setText("Address Book");
  
  {
  
  newButton = new Button(shell, SWT.NONE);
  
  newButton.addSelectionListener(new SelectionAdapter() {
  
  public void widgetSelected(SelectionEvent e) {
  
  clearText();
  
  setTextEditable(true);
  
  enableEditButtons(false);
  
  enableSaveButtons(true);
  
  System.out.println("New button selected.");
  
  }
  
  });
  
  newButton.setBounds(10, 380, 75, 35);
  
  newButton.setText("New");
  
  }
  
  {
  
  deleteButton = new Button(shell, SWT.NONE);
  
  deleteButton.addSelectionListener(new SelectionAdapter() {
  
  public void widgetSelected(SelectionEvent e) {
  
  clearText();
  
  System.out.println("Delete button selected.");
  
  }
  
  });
  
  deleteButton.setBounds(85, 380, 75, 35);
  
  deleteButton.setText("Delete");
  
  }
  
  {
  
  editButton = new Button(shell, SWT.NONE);
  
  editButton.addSelectionListener(new SelectionAdapter() {
  
  public void widgetSelected(SelectionEvent e) {
  
  setTextEditable(true);
  
  enableEditButtons(false);
  
  enableSaveButtons(true);
  
  System.out.println("Edit button selected.");
  
  }
  
  });
  
  editButton.setBounds(160, 380, 75, 35);
  
  editButton.setText("Edit");
  
  }
  
  {
  
  prevButton = new Button(shell, SWT.NONE);
  
  prevButton.addSelectionListener(new SelectionAdapter() {
  
  public void widgetSelected(SelectionEvent e) {
  
  System.out.println("Previous button selected.");
  
  }
  
  });
  
  prevButton.setBounds(265, 380, 75, 35);
  
  prevButton.setText("Previous");
  
  }
  
  {
  
  nextButton = new Button(shell, SWT.NONE);
  
  nextButton.addSelectionListener(new SelectionAdapter() {
  
  public void widgetSelected(SelectionEvent e) {
  
  System.out.println("Next button selected.");
  
  }
  
  });
  
  nextButton.setBounds(340, 380, 75, 35);
  
  nextButton.setText("Next");
  
  }
  
  {
  
  saveButton = new Button(shell, SWT.NONE);
  
  saveButton.addSelectionListener(new SelectionAdapter() {
  
  public void widgetSelected(SelectionEvent e) {
  
  setTextEditable(false);
  
  enableEditButtons(true);
  
  enableSaveButtons(false);
  
  System.out.println("Save button selected.");
  
  }
  
  });
  
  saveButton.setBounds(445, 380, 75, 35);
  
  saveButton.setText("Save");
  
  saveButton.setEnabled(false);
  
  }
  
  {
  
  cancelButton = new Button(shell, SWT.NONE);
  
  cancelButton.addSelectionListener(new SelectionAdapter() {
  
  public void widgetSelected(SelectionEvent e) {
  
  setTextEditable(false);
  
  enableEditButtons(true);
  
  enableSaveButtons(false);
  
  System.out.println("Cancel button selected.");
  
  }
  
  });
  
  cancelButton.setBounds(520, 380, 75, 35);
  
  cancelButton.setText("Cancel");
  
  cancelButton.setEnabled(false);
  
  }
  
  {
  
  final Group group = new Group(shell, SWT.NONE);
  
  group.setText("Details");
  
  group.setBounds(10, 10, 585, 355);
  
  {
  
  final Label label = new Label(group, SWT.NONE);
  
  label.setBounds(10, 20, 135, 25);
  
  label.setText("First Name:");
  
  }
  
  {
  
  final Label label = new Label(group, SWT.NONE);
  
  label.setBounds(10, 60, 135, 25);
  
  label.setText("Last Name:");
  
  }
  
  {
  
  final Label label = new Label

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    闂傚倸鍊搁崐椋庢閿熺姴鐭楅幖娣妼缁愭鏌¢崶鈺佷汗闁哄閰i弻鏇$疀鐎n亞浠炬繝娈垮灠閵堟悂寮婚弴锛勭杸閻庯綆浜栭崑鎾诲冀椤撱劎绋忛梺璺ㄥ櫐閹凤拷

    濠电姷鏁告慨鐑姐€傛禒瀣劦妞ゆ巻鍋撻柛鐔锋健閸┾偓妞ゆ巻鍋撶紓宥咃躬楠炲啫螣鐠囪尙绐為梺褰掑亰閸撴盯鎮惧ú顏呪拺闂傚牊鍗曢崼銉ョ柧婵犲﹤瀚崣蹇旂節婵犲倻澧涢柛瀣ㄥ妽閵囧嫰寮介妸褋鈧帡鏌熼挊澶婃殻闁哄瞼鍠栭幃婊堝煛閸屾稓褰嬮柣搴ゎ潐濞叉ê鐣濈粙璺ㄦ殾闁割偅娲栭悡娑㈡煕鐏炲墽鐭嬫繛鍫熸倐濮婄粯鎷呯粵瀣異闂佹悶鍔嬮崡鍐茬暦閵忋倕鍐€妞ゆ劑鍎卞皬闂備焦瀵х粙鎴犫偓姘煎弮瀹曚即宕卞Ο闀愮盎闂侀潧鐗嗛幊搴㈡叏椤掆偓閳规垿鍩ラ崱妞剧凹濠电姰鍨洪敋閾荤偞淇婇妶鍛櫤闁稿鍊圭换娑㈠幢濡纰嶉柣搴㈣壘椤︾敻寮诲鍫闂佸憡鎸鹃崰搴敋閿濆鏁嗗〒姘功閻绻涢幘鏉戠劰闁稿鎹囬弻锝呪槈濞嗘劕纾抽梺鍝勬湰缁嬫垿鍩為幋锕€宸濇い鏇炴噺閳诲﹦绱撻崒娆戝妽妞ゃ劌鎳橀幆宀勫磼閻愰潧绁﹂柟鍏肩暘閸斿矂鎮為崹顐犱簻闁圭儤鍨甸鈺呮倵濮橆剦妲归柕鍥у瀵粙濡歌閸c儳绱撴担绛嬪殭婵☆偅绻堝濠氭偄绾拌鲸鏅i悷婊冪Ч閹﹢鎳犻鍌滐紲闁哄鐗勯崝搴g不閻愮儤鐓涢悘鐐跺Г閸犳﹢鏌℃担鐟板鐎规洜鍠栭、姗€鎮╅搹顐ら拻闂傚倷娴囧畷鍨叏閹惰姤鈷旂€广儱顦崹鍌炴煢濡尨绱氶柨婵嗩槸缁€瀣亜閺嶃劎鈽夋繛鍫熺矒濮婅櫣娑甸崨顔俱€愬銈庡亝濞茬喖宕洪埀顒併亜閹哄棗浜鹃梺鎸庢穿婵″洤危閹版澘绫嶉柛顐g箘椤撴椽姊虹紒妯哄鐎殿噮鍓欒灃闁告侗鍠氶崢鎼佹⒑閸撴彃浜介柛瀣閹﹢鏁冮崒娑氬幈闁诲函缍嗛崑鍡樻櫠椤掑倻纾奸柛灞剧☉缁椦囨煙閻熸澘顏柟鐓庢贡閹叉挳宕熼棃娑欐珡闂傚倸鍊风粈渚€骞栭銈傚亾濮樺崬鍘寸€规洖缍婇弻鍡楊吋閸涱垽绱遍柣搴$畭閸庨亶藝娴兼潙纾跨€广儱顦伴悡鏇㈡煛閸ャ儱濡煎褜鍨伴湁闁绘ǹ绉鍫熺畳闂備焦瀵х换鍌毼涘Δ鍛厺闁哄洢鍨洪悡鍐喐濠婂牆绀堟慨妯挎硾閽冪喖鏌曟繛褍瀚烽崑銊╂⒑缂佹ê濮囨い鏇ㄥ弮閸┿垽寮撮姀鈥斥偓鐢告煥濠靛棗鈧懓鈻嶉崶銊d簻闊洦绋愰幉楣冩煛鐏炵偓绀嬬€规洟浜堕、姗€鎮㈡總澶夌处

    重磅专题
    往期文章
    最新文章