eclipse插件開發用到圖形界面的時候須要SWT,所以這裏記錄下開發環境而後寫一個demo以便之後快速搭建工做環境:java
關於開發環境,本人也看了幾篇帖子,要安裝個什麼windowsbuilder,網址http://download.eclipse.org/windowbuilder/WB/release/R201309271200/3.7/shell
可是昨晚測試了一下,不知道是否是網絡問題,沒有能快速下載,而後找csdn下載了一個,選擇archive安裝,可是仍是很差使,以爲不必折騰,就找了個現成的,已經搭建好環境的eclipse3.5,傳上去之後直接拿來用,http://pan.baidu.com/s/1c0loOsKwindows
主要仍是有個界面能夠拖拽一下位置,網絡
建議不要用它來拖出組件,由於會生成一個
eclipse
private static final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
這樣一來會和我下面的代碼產生衝突,這裏若是是SWT高手可能會對我進行批評,由於個人代碼比較生硬,我確實沒搞過SWT,因此僅僅是能用,沒有考慮質量的問題,建議新手就像我這樣來,避免沒必要要的麻煩:ide
package com.xjz.genecode; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.DirectoryDialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.eclipse.wb.swt.SWTResourceManager; public class UIMain { public static void main(String[] args) { /** * 初始化Display和Shell */ Display display = new Display(); final Shell shell = new Shell(display, SWT.CLOSE); shell.setSize(600, 200); int[] location = getCenterLocation(shell);//居中 shell.setLocation(location[0], location[1]); shell.setText("代碼生成");//窗口標題 /** * XML路徑選擇欄 */ Label lblXMLSrc = new Label(shell, SWT.CENTER); lblXMLSrc.setFont(SWTResourceManager.getFont("微軟雅黑", 12, SWT.NORMAL)); lblXMLSrc.setLocation(29, 38); lblXMLSrc.setSize(130, 23); lblXMLSrc.setText("\u9009\u62E9XML\u8DEF\u5F84\uFF1A"); final Text txtXMLSrc = new Text(shell, SWT.NONE); txtXMLSrc.setBounds(183, 38, 259, 23); Button btnXMLSrc = new Button(shell, SWT.NONE); btnXMLSrc.setBounds(463, 37, 80, 27); btnXMLSrc.setText("\u9009\u62E9"); btnXMLSrc.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { DirectoryDialog dirDialog = new DirectoryDialog(shell, SWT.OPEN); dirDialog.setFilterPath("SystemRoot"); txtXMLSrc.setText(dirDialog.open()); } }); /** * 代碼生成路徑指向欄 */ Label lblCodeTo = new Label(shell, SWT.CENTER); lblCodeTo.setFont(SWTResourceManager.getFont("微軟雅黑", 12, SWT.NORMAL)); lblCodeTo.setLocation(29, 74); lblCodeTo.setSize(130, 23); lblCodeTo.setText("\u751F\u6210\u8DEF\u5F84\u6307\u5411\uFF1A"); final Text txtCodeTo = new Text(shell, SWT.NONE); txtCodeTo.setBounds(183, 74, 259, 23); Button btnCodeTo = new Button(shell, SWT.NONE); btnCodeTo.setBounds(463, 73, 80, 27); btnCodeTo.setText("\u9009\u62E9"); btnCodeTo.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { DirectoryDialog dirDialog = new DirectoryDialog(shell, SWT.OPEN); dirDialog.setFilterPath("SystemRoot"); txtCodeTo.setText(dirDialog.open()); } }); /** * 按鈕一鍵生成代碼 */ Button createCodeBtn = new Button(shell, SWT.CENTER); createCodeBtn.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { Main.geneCode(txtXMLSrc.getText() ,txtCodeTo.getText()); } }); createCodeBtn.setText("\u4E00\u952E\u751F\u6210"); createCodeBtn.setBounds(170, 120, 259, 27); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } private static int[] getCenterLocation(Shell shell) { int width = shell.getMonitor().getClientArea().width; int height = shell.getMonitor().getClientArea().height; int x = shell.getSize().x; int y = shell.getSize().y; if (x > width) { shell.getSize().x = width; } if (y > height) { shell.getSize().y = height; } int[] locations = new int[2]; locations[0] = (width - x) / 2; locations[1] = (height - y) / 2; return locations; } }
運行結果:測試
解釋必要點:ui
SWT程序入口就是Display和Shell,因此一開始就建立好固定的寫法,而後要什麼按鈕控件本身再往上添加便可。spa