Java語言的聲望和它在桌面應(yīng)用程序(GUI程序)所取得的成就顯然極不相符,至今仍然很少能看到非常成功Java桌面程序。雖然有JBuilder,Netbean,JProbe等大型軟件作為代表,但這仍不能證明Java的GUI程序是成功的:它們的外觀總是和同一操作系統(tǒng)平臺下的其它軟件顯得格格不入。對機器配置的需求也似乎永無止境,這使得它們只能被一些總是擁有當(dāng)前最高性能PC的程序員們所容忍,或是那些不在乎金錢和時間的專業(yè)用戶所接受。對絕大多數(shù)計算機使用者來說,AWT或SWING代表著怪異的界面和無法接受的速度。Standard Widget Toolkit(SWT)或許是Java這一噩夢的終結(jié)者,廣大Java程序員終于可以開發(fā)出高效率的GUI程序,它們擁有標(biāo)準的外觀,幾乎沒有人能看出你的程序是用Java寫出來的,更為重要的是,這些程序是跨平臺的。
  
  SWT本身僅僅是Eclipse組織為了開發(fā)Eclipse IDE環(huán)境所編寫的一組底層圖形界面 API。或許是無心插柳,或是有意為之,至今為止,SWT無論是在性能和外觀上,都超越了SUN公司提供的AWT和SWING。目前Eclipse IDE已經(jīng)開發(fā)到了2.1版本,SWT已經(jīng)十分穩(wěn)定。這里指的穩(wěn)定應(yīng)該包含兩層意思:
  
  一、是指性能上的穩(wěn)定,其中的關(guān)鍵是源于SWT的設(shè)計理念。
  
  SWT最大化了操作系統(tǒng)的圖形構(gòu)件API,就是說只要操作系統(tǒng)提供了相應(yīng)圖形的構(gòu)件,那么SWT只是簡單應(yīng)用JNI技術(shù)調(diào)用它們,只有那些操作系統(tǒng)中不提供的構(gòu)件,SWT才自己去做一個模擬的實現(xiàn)。可以看出SWT的性能上的穩(wěn)定大多時候取決于相應(yīng)操作系統(tǒng)圖形構(gòu)件的穩(wěn)定性。
  
  另一個穩(wěn)定是指SWT API包中的類、方法的名稱和結(jié)構(gòu)已經(jīng)少有改變,程序員不用擔(dān)心由于Eclipse組織開發(fā)進度很快(Eclipse IDE每天都會有一個Nightly版本的發(fā)布),而導(dǎo)致自己的程序代碼變化過大。從一個版本的SWT更新至另一版本,通常只需要簡單將SWT包換掉就可以了。
  
  二、Eclipse3.0的swt編程
  
  1.swt比awt和swing要快多,因為它是利用操作系統(tǒng)的界面組件生成UI的,在java桌面設(shè)計領(lǐng)域掀起一場革命
  
  2.環(huán)境配置:
  windows系統(tǒng)+eclipse3.0
  
  3.具體操作:
  (1).新建一java項目,命名swt,文件結(jié)構(gòu)如下:
  +swt
  +bin(編譯輸出)
  +src(原文件)
  +AddressBookUI.java
  +swt-awt-win32-3062.dll(以下均從eclipse\plugins\org.eclipse.swt.win32_3.0.1\os\win32\x86下導(dǎo)入)
  +swt-win32-3062.dll
  +javaw.exe.manifest
  
  (2).到項目的properties里,在java build path | libraries里將swt.jar導(dǎo)入
  
  (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(group, SWT.NONE);
  label.setBounds(10, 100, 135, 25);
  label.setText("Phone:");
  }
  {
  final Label label = new Label(group, SWT.NONE);
  label.setBounds(10, 140, 135, 25);
  label.setText("Email:");
  }
  {
  final Label label = new Label(group, SWT.NONE);
  label.setBounds(10, 180, 135, 25);
  label.setText("Address:");
  }
  {
  final Label label = new Label(group, SWT.NONE);
  label.setBounds(10, 255, 135, 25);
  label.setText("Miscellaneous Information:");
  }
  {
  fnameText = new Text(group, SWT.BORDER | SWT.READ_ONLY);
  fnameText.setBounds(150, 15, 420, 25);
  }
  {
  lnameText = new Text(group, SWT.BORDER | SWT.READ_ONLY);
  lnameText.setBounds(150, 55, 420, 25);
  }
  {
  phoneText = new Text(group, SWT.BORDER | SWT.READ_ONLY);
  phoneText.setBounds(150, 95, 420, 25);
  }
  {
  emailText = new Text(group, SWT.BORDER | SWT.READ_ONLY);
  emailText.setBounds(150, 135, 420, 25);
  }
  {
  addrText = new Text(group, SWT.BORD