熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

基於Eclipse 3.0的SWT編程

2013-11-23 20:14:38  來源: Java開源技術 
SWT簡介

  Java語言的聲望和它在桌面應用程序(GUI程序)所取得的成就顯然極不相符至今仍然很少能看到非常成功Java桌面程序雖然有JBuilderNetbeanJProbe等大型軟件作為代表但這仍不能證明Java的GUI程序是成功的它們的外觀總是和同一操作系統平台下的其它軟件顯得格格不入對機器配置的需求也似乎永無止境這使得它們只能被一些總是擁有當前最高性能PC的程序員們所容忍或是那些不在乎金錢和時間的專業用戶所接受對絕大多數計算機使用者來說AWT或SWING代表著怪異的界面和無法接受的速度Standard Widget Toolkit(SWT)或許是Java這一噩夢的終結者廣大Java程序員終於可以開發出高效率的GUI程序它們擁有標准的外觀幾乎沒有人能看出你的程序是用Java寫出來的更為重要的是這些程序是跨平台的

  SWT本身僅僅是Eclipse組織為了開發Eclipse IDE環境所編寫的一組底層圖形界面 API或許是無心插柳或是有意為之至今為止SWT無論是在性能和外觀上都超越了SUN公司提供的AWT和SWING目前Eclipse IDE已經開發到了版本SWT已經十分穩定這裡指的穩定應該包含兩層意思

  一是指性能上的穩定其中的關鍵是源於SWT的設計理念SWT最大化了操作系統的圖形構件API就是說只要操作系統提供了相應圖形的構件那麼SWT只是簡單應用JNI技術調用它們只有那些操作系統中不提供的構件SWT才自己去做一個模擬的實現可以看出SWT的性能上的穩定大多時候取決於相應操作系統圖形構件的穩定性

  另一個穩定是指SWT API包中的類方法的名稱和結構已經少有改變程序員不用擔心由於Eclipse組織開發進度很快(Eclipse IDE每天都會有一個Nightly版本的發布)而導致自己的程序代碼變化過大從一個版本的SWT更新至另一版本通常只需要簡單將SWT包換掉就可以了

  二Eclipse的SWT編程

  SWT比AWT和Swing要快多因為它是利用操作系統的界面組件生成UI的在java桌面設計領域掀起一場革命

  環境配置

  windows系統+eclipse

  具體操作

  ()新建一java項目命名SWT文件結構如下

  +swt
  +bin(編譯輸出)
  +src(原文件)
  +AddressBookUIjava
  +swtawtwindll(以下均從eclipse\plugins\orgeclipseswtwin_\os\win\x下導入)
  +swtwindll
  +javawexemanifest

  ()到項目的properties裡在java build path | libraries裡將swtjar導入

  ()AddressBookUIjava原代碼如下

import orgeclipseswtwidgetsDisplay;
import orgeclipseswtwidgetsShell;
import orgeclipseswtSWT;
import orgeclipseswtwidgetsButton;
import orgeclipseswtwidgetsGroup;
import orgeclipseswtwidgetsLabel;
import orgeclipseswtwidgetsText;
import orgeclipseswtwidgets*;
import orgeclipseswteventsSelectionAdapter;
import orgeclipseswteventsSelectionEvent;
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();
  windowopen();
 }
 public void open() {
  final Display display = new Display();
  shell = new Shell();
  shellsetSize( );
  shellsetText(Address Book);
 {
  newButton = new Button(shell SWTNONE);
  newButtonaddSelectionListener(new SelectionAdapter() {
   public void widgetSelected(SelectionEvent e) {
    clearText();
    setTextEditable(true);
    enableEditButtons(false);
    enableSaveButtons(true);

    Systemoutprintln(New button selected);
   }
  });
  newButtonsetBounds( );
  newButtonsetText(New);
 }
 {
  deleteButton = new Button(shell SWTNONE);
  deleteButtonaddSelectionListener(new SelectionAdapter() {
   public void widgetSelected(SelectionEvent e) {
    clearText();

    Systemoutprintln(Delete button selected);
   }
  });
  deleteButtonsetBounds( );
  deleteButtonsetText(Delete);
 }
 {
  editButton = new Button(shell SWTNONE);
  editButtonaddSelectionListener(new SelectionAdapter() {
   public void widgetSelected(SelectionEvent e) {
    setTextEditable(true);
    enableEditButtons(false);
    enableSaveButtons(true);
  
    Systemoutprintln(Edit button selected);
   }
  });
  editButtonsetBounds( );
  editButtonsetText(Edit);
 }
 {
  prevButton = new Button(shell SWTNONE);
  prevButtonaddSelectionListener(new SelectionAdapter() {
   public void widgetSelected(SelectionEvent e) {
    Systemoutprintln(Previous button selected);
   }
  });
  prevButtonsetBounds( );
  prevButtonsetText(Previous);
 }
 {
  nextButton = new Button(shell SWTNONE);
  nextButtonaddSelectionListener(new SelectionAdapter() {
   public void widgetSelected(SelectionEvent e) {
    Systemoutprintln(Next button selected);
   }
  });
  nextButtonsetBounds( );
  nextButtonsetText(Next);
 }
 {
  saveButton = new Button(shell SWTNONE);
  saveButtonaddSelectionListener(new SelectionAdapter() {
   public void widgetSelected(SelectionEvent e) {
    setTextEditable(false);
    enableEditButtons(true);
    enableSaveButtons(false);
 
    Systemoutprintln(Save button selected);
   }
  });
  saveButtonsetBounds( );
  saveButtonsetText(Save);
  saveButtonsetEnabled(false);
 }
 {
  cancelButton = new Button(shell SWTNONE);
  cancelButtonaddSelectionListener(new SelectionAdapter() {
   public void widgetSelected(SelectionEvent e) {
    setTextEditable(false);
    enableEditButtons(true);
    enableSaveButtons(false);

    Systemoutprintln(Cancel button selected);
   }
  });
  cancelButtonsetBounds( );
  cancelButtonsetText(Cancel);
  cancelButtonsetEnabled(false);
 }
 {
  final Group group = new Group(shell SWTNONE);
  groupsetText(Details);
  groupsetBounds( );
  {
   final Label label = new Label(group SWTNONE);
   labelsetBounds( );
   labelsetText(First Name:);
  }
  {
   final Label label = new Label(group SWTNONE);
   labelsetBounds( );
   labelsetText(Last Name:);
  }
  {
   final Label label = new Label(group SWTNONE);
   labelsetBounds( );
   labelsetText(Phone:);
  }
  {
   final Label label = new Label(group SWTNONE);
   labelsetBounds( );
   labelsetText(Email:);
  }
  {
   final Label label = new Label(group SWTNONE);
   labelsetBounds( );
   labelsetText(Address:);
  }
  {
   final Label label = new Label(group SWTNONE);
   labelsetBounds( );
   labelsetText(Miscellaneous Information:);
  }
  {
   fnameText = new Text(group SWTBORDER | SWTREAD_ONLY);
   fnameTextsetBounds( );
  }
  {
   lnameText = new Text(group SWTBORDER | SWTREAD_ONLY);
   lnameTextsetBounds( );
  }
  {
   phoneText = new Text(group SWTBORDER | SWTREAD_ONLY);
   phoneTextsetBounds( );
  }
  {
   emailText = new Text(group SWTBORDER | SWTREAD_ONLY);
   emailTextsetBounds( );
  }
  {
   addrText = new Text(group SWTBORDER | SWTREAD_ONLY | SWTV_SCROLL);
   addrTextsetBounds( );
  }
  {
   miscText = new Text(group SWTBORDER | SWTREAD_ONLY | SWTV_SCROLL);
   miscTextsetBounds( );
  }
 }

 setupMenu();

 shellopen();
 while (!shellisDisposed()) {
  if (!displayreadAndDispatch())
   displaysleep();
 }
}

private void setupMenu() {
 //create the menu bar
 Menu menu = new Menu(shell SWTBAR);
 shellsetMenuBar(menu);
 //add the File option to it
 MenuItem file = new MenuItem(menu SWTCASCADE);
 filesetText(File);
 //create a menu for the File option
 filesetMenu(new Menu(file));
 //add MenuItems to the File menu
 MenuItem prevItem = new MenuItem(new Menu(file) SWTNONE);
 prevItemsetText(Previous);

 MenuItem nextItem = new MenuItem(new Menu(file) SWTPUSH);
 nextItemsetText(Next);

 MenuItem seperator = new MenuItem(new Menu(file) SWTSEPARATOR);
 MenuItem quitItem = new MenuItem(new Menu(file) SWTPUSH);
 quitItemsetText(Quit);
 //add listeners for the actions
 prevItemaddListener(SWTSelection new Listener() {
  public void handleEvent(Event e) {
   Systemoutprintln(Previous menu item selected);
  }
 });
 nextItemaddListener(SWTSelection new Listener() {
  public void handleEvent(Event e) {
   Systemoutprintln(Next menu item selected);
  }
 });
 quitItemaddListener(SWTSelection new Listener() {
  public void handleEvent(Event e) {
   shelldispose();
  }
 });
}

private void clearText() {
 fnameTextsetText();
 lnameTextsetText();
 phoneTextsetText();
 emailTextsetText();
 addrTextsetText();
 miscTextsetText();
}

private void setTextEditable(boolean editable) {
 fnameTextsetEditable(editable);
 lnameTextsetEditable(editable);
 phoneTextsetEditable(editable);
 emailTextsetEditable(editable);
 addrTextsetEditable(editable);
 miscTextsetEditable(editable);
}

private void enableEditButtons(boolean enable) {
 newButtonsetEnabled(enable);
 deleteButtonsetEnabled(enable);
 editButtonsetEnabled(enable);
}

private void enableSaveButtons(boolean enable) {
 saveButtonsetEnabled(enable);
 cancelButtonsetEnabled(enable);
}
}
  ()測試運行run就可以啦
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28176.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.