?? 前段時(shí)間用 eclipse 的插件 SWT 做了一個(gè)簡(jiǎn)單的應(yīng)用程序界面,感覺沒有 VC 做界面方便和強(qiáng)大,還不時(shí)的和 AWT 沖突,也可能是剛學(xué),沒有體會(huì) SWT 的優(yōu)點(diǎn),我把自己的掌握的寫出來,大家一起交流哈。
?? 好了,現(xiàn)在言歸正傳,開始哈。
1.
安裝
Designer_v5.0.0 _for_Eclipse3.0
根據(jù) eclipse 版本的不同,下載不同的 SWT-Desiger. 下載地址: http://www.swt-designer.com/
安裝方法和所有的 eclipse 插件一樣,將 features, plugins 下的文件放到 eclipse 相應(yīng)的文件夾中就好了,重啟 eclipse, OK .
2.
破解
Designer_v5.0.0 _for_Eclipse3.0
eclipse->windows->preferences->Designer –Registration and Activation ->SWT Designer Professional Paid ->next->
輸入相關(guān)的信息
->next
在
serial Number
和
Activation Key
輸入注冊(cè)碼。注冊(cè)機(jī)的下載地址:
http://www.aygfsteel.com/Files/zjuedsion/解壓并且修改好的破解工具.rar
next 就 OK 了。
3. 新建一個(gè) Test 工程
File- >project-> Designer SWT/JFace Java Project
建好工程后要導(dǎo)入 SWT 的原生庫(kù),要不然,項(xiàng)目運(yùn)行時(shí)會(huì)抱錯(cuò): Java.lang.UnsatifiledLinkError. no swt-awt-win32-3064.dll in java.Labrary.path 我這里是 swt-awt-win32-3064.dll ,可能版本會(huì)有點(diǎn)不一樣。導(dǎo)入的方法:選擇項(xiàng)目名,點(diǎn)右鍵, Import->File System 如下圖
?
一般
swt-awt-win32-3064.dll
在
x:\eclipse\plugins\org.eclipse.swt.win32_
?4.
建
SWT Application
File->new ->others->Designer->SWT->Application Window
package :? com.swtdesign
name: Myapp
create contents in : public static main() method
如下圖
?
點(diǎn) Finish
就好了
看代碼
package com.swtdesigner; //
包名
import org.eclipse.swt.widgets.Display;//
程序所用到的類都會(huì)用
import
標(biāo)記在這里,
import org.eclipse.swt.widgets.Shell;??? //import
的快捷鍵
Ctrl+Shift+O
public class Test{? //
一個(gè)標(biāo)準(zhǔn)的
Java
類
HelloWorld
??? public static void main(String[] args) {
?????????? //display
負(fù)責(zé)管理事件循環(huán)和控制
UI
線程和其他線程之間的通訊。
??????? final Display display = Display.getDefault();?
??????? final Shell shell = new Shell();? // shell
是程序的主窗口
??????? shell.setSize(327, 253);? //
設(shè)置主窗口的大小
??????? shell.setText("SWT Application");? //
設(shè)置主窗口的標(biāo)題
??????? shell.layout();? //shell
應(yīng)用界面布置
??????? shell.open();? //
打開
shell
主窗口
??????? while (!shell.isDisposed()) { //
如果主窗口沒有關(guān)閉,則一直循環(huán)
??????????? if (!display.readAndDispatch())? //
如果
display
不忙
??????????????? display.sleep(); //display
休眠
??????? }
??? }
}
從這個(gè)代碼可以看到,創(chuàng)建一個(gè)典型的SWT應(yīng)用程序需要以下步驟:
l??????????
創(chuàng)建一個(gè)
Display
l??????????
創(chuàng)建一個(gè)或多個(gè)
Shell
l??????????
設(shè)置
Shell
的布局
l??????????
創(chuàng)建
Shell
中的的組件(注:本例還沒有加入組件,只是一個(gè)空窗口)
l??????????
用
open()
方法打開
Shell
窗口
l??????????
寫一個(gè)事件轉(zhuǎn)發(fā)循環(huán)
l??????????
銷毀
display
接下來我們?cè)?/span> 應(yīng)用程序上加一個(gè) Button 并制作他的 單擊事件。事件的內(nèi)容是調(diào)出一個(gè)提示窗口,和一個(gè)文本框,接受外面的輸入。
如下圖在,切換到 design 模式,加一個(gè) button ,在左邊的菜單里選一個(gè) button ,然后直接點(diǎn)在面板上,不是拖過去。
可以直接在這里修改屬性
雙擊
button
為其添加
selection
事件,也可以點(diǎn)右鍵-
>implement->selection-
>widgetSelected
然后切換到 source 模式,看到增加了以下代碼
?????????????
final Button button = new Button(shell, SWT.NONE);
// 這就是增加的鼠標(biāo)單擊事件代碼,以匿名內(nèi)部類的方式寫的
????????????? button.addSelectionListener(new SelectionAdapter() {
// 要實(shí)現(xiàn)事件,必須實(shí)現(xiàn) widgetSelected 方法
???????????????????? public void widgetSelected(SelectionEvent e) {
// 信息提示對(duì)話框
MessageDialog.openInformation(null,"hello world","hello");
???????????????????? }
????????????? });
?????????????
我們可以將匿名內(nèi)部類,改成不是匿名的
將原來的事件代碼改為:
????????????? button.addSelectionListener(new addButtonSelected());
同時(shí)加 addButtonSelected 內(nèi)部類
// 加 static 使其可以直接 new ,繼承 SelectionAdapter ,直接寫方法就可以了
public static class? addButtonSelected extends SelectionAdapter
?????? {
????????????? public void widgetSelected(SelectionEvent e) {
???????????????????? MessageDialog.openInformation(null,"hello world","hello");
????????????? }
?????? }
好了這樣就實(shí)現(xiàn)了加一個(gè) button ,并單擊相應(yīng)一個(gè)事件。夠簡(jiǎn)單了吧。下面加一個(gè)文本框
同理切換到 Design 模式下加一個(gè)名為 input_Text 的文本框,在文本框左邊加個(gè) Clable 的標(biāo)簽“輸入”,再加一個(gè) button(input_button) ,點(diǎn)確定后接受輸入。然后加一組輸出現(xiàn)實(shí), output_Text. , output_button
如下圖
?
然后加響應(yīng)代碼;
?????? // 把文本框的內(nèi)容和一個(gè) String 變量綁定
?????? private static String? output_string=null;
// 輸入輸出事件
input_button.addSelectionListener(new inputText());
output_button.addSelectionListener(new outputText());
// 輸入事件
?????? public static?? class? inputText extends SelectionAdapter
?????? {
????????????? public void widgetSelected(SelectionEvent e)
????????????? {
???????????????????? output_string=input_text.getText();
????????????????????
???????????????????? System.out.println(output_string);
????????????? }
??????
?????? }
//???? 輸入事件
?????? public static?? class? outputText extends SelectionAdapter
?????? {
????????????? public void widgetSelected(SelectionEvent e)
????????????? {
???????????????????? output_text.setText(output_string);
????????????????????
????????????????????
???????????????????? System.out.println(output_string);
????????????? }
??????
?????? }
最后的效果如下圖
OK!
結(jié)束。如有問題,請(qǐng)聯(lián)系
edsionchen_zju@hotmal.com
看這里的注冊(cè)方法,我6.0.0也成功了。