一、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?;蛟S是無心插柳,或是有意為之,至今為止,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(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.BORDER | SWT.READ_ONLY | SWT.V_SCROLL); addrText.setBounds(150, 175, 420, 60); } { miscText = new Text(group, SWT.BORDER | SWT.READ_ONLY | SWT.V_SCROLL); miscText.setBounds(150, 250, 420, 65); } } setupMenu(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } private void setupMenu() { //create the menu bar Menu menu = new Menu(shell, SWT.BAR); shell.setMenuBar(menu); //add the File option to it MenuItem file = new MenuItem(menu, SWT.CASCADE); file.setText("File"); //create a menu for the File option file.setMenu(new Menu(file)); //add MenuItems to the File menu MenuItem prevItem = new MenuItem(new Menu(file), SWT.NONE); prevItem.setText("Previous"); MenuItem nextItem = new MenuItem(new Menu(file), SWT.PUSH); nextItem.setText("Next"); MenuItem seperator = new MenuItem(new Menu(file), SWT.SEPARATOR); MenuItem quitItem = new MenuItem(new Menu(file), SWT.PUSH); quitItem.setText("Quit"); //add listeners for the actions prevItem.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { System.out.println("Previous menu item selected."); } }); nextItem.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { System.out.println("Next menu item selected."); } }); quitItem.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { shell.dispose(); } }); } private void clearText() { fnameText.setText(""); lnameText.setText(""); phoneText.setText(""); emailText.setText(""); addrText.setText(""); miscText.setText(""); } private void setTextEditable(boolean editable) { fnameText.setEditable(editable); lnameText.setEditable(editable); phoneText.setEditable(editable); emailText.setEditable(editable); addrText.setEditable(editable); miscText.setEditable(editable); } private void enableEditButtons(boolean enable) { newButton.setEnabled(enable); deleteButton.setEnabled(enable); editButton.setEnabled(enable); } private void enableSaveButtons(boolean enable) { saveButton.setEnabled(enable); cancelButton.setEnabled(enable); } }