首先自我介紹下,本人非專職IT人員、純粹是IT愛好者,也並非計算機專業畢業的,所用的Java知識也全憑空閒時間學習的,所以在很多地方代碼不夠精煉,也只是能實現
一些功能吧,當然也參考了一些前輩的文檔等。寫此篇博客也純粹是把自己學到的寫寫筆記而已。好了廢話不多說,開始吧!
首先簡單介紹下SWT吧。在做這個局域網QQ時,爲什麼選擇SWT估計稍微對Java有點了解的都知道Java用自帶的AWT/SWING做桌面程序確實不咋的。而IBM開發的SWT在視覺上超過了它們了;然後有個eclipse有個插件(Jigloo,大家可以自行查找)安裝後可以實現類似微軟的VS一樣拖動控件(畢竟什麼都手寫的話太累是吧?)。出於這樣吧,就選擇了SWT——雖然也是第一次接觸。至於SWT相關的知識,也請大家去百度瞭解。
1、插件的安裝與使用
說到這裏,推薦大家去看篇博客http://blog.csdn.net/tangl_99/article/details/1396805。插件的安裝已經說了,這裏就直接來使用了,步驟如下:
1、新建一個Java工程,然後在src文件夾下建other
2、找到GUI Forms下的SWT(如果沒有確認你的插件安裝正確)
3、包名、類名自己寫。注意紅色的圈是SWT運行所需要的庫
4、建好後就是這樣的界面,這樣對於一些控件你可以拖動到窗體上,然後寫相關的事件代碼即可。
5、寫一個匿名內部類用來彈出一個對話框,看是不是比難看的AWT好多了?
接下來看看代碼吧:
package Main;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import com.cloudgarden.resource.SWTResourceManager;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewSWTApp extends org.eclipse.swt.widgets.Composite {
private Button btnOK;
{
//Register as a resource user - SWTResourceManager will
//handle the obtaining and disposing of resources
SWTResourceManager.registerResourceUser(this);
}
public NewSWTApp(Composite parent, int style) {
super(parent, style);
initGUI();
}
/**
* 界面初始化。主要的工作是界面的佈局、窗體中的控件佈局等
*/
private void initGUI() {
try {
this.setSize(new org.eclipse.swt.graphics.Point(400,300));
this.setBackground(SWTResourceManager.getColor(6,157,213));
FormLayout thisLayout = new FormLayout();
this.setLayout(thisLayout);
{
btnOK = new Button(this, SWT.PUSH | SWT.CENTER);
FormData btnOKLData = new FormData();
btnOKLData.left = new FormAttachment(0, 1000, 119);
btnOKLData.top = new FormAttachment(0, 1000, 86);
btnOKLData.width = 36;
btnOKLData.height = 27;
btnOK.setLayoutData(btnOKLData);
btnOK.setText("確定");
btnOK.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent evt) {
MessageBox box = new MessageBox(new Shell());
box.setMessage("你好,SWT!");
box.open();
}
});
}
this.layout();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* main方法,用來生成主窗體,大小、顯示等。注意SWT中不一樣,首先有個Display和Shell,然後用它創建主界面。
* Auto-generated main method to display this
* org.eclipse.swt.widgets.Composite inside a new Shell.
*/
public static void main(String[] args) {
Display display = Display.getDefault();
Shell shell = new Shell(display);
NewSWTApp inst = new NewSWTApp(shell, SWT.NULL);
Point size = inst.getSize();
shell.setLayout(new FillLayout());
shell.layout();
if(size.x == 0 && size.y == 0) {
inst.pack();
shell.pack();
} else {
Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
shell.setSize(shellBounds.width, shellBounds.height);
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
好了就這樣。。下面就把我做的半成品貼出來吧,讓大家見見。
還是那麼像樣,呵呵~