利用SWT做Java版局域网QQ(一)——基于UDP协议 – Huang

首先自我介绍下,本人非专职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 resourcesSWTResourceManager.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();}}}

好了就这样。。下面就把我做的半成品贴出来吧,让大家见见。

还是那么像样,呵呵~

获致幸福的不二法门是珍视你所拥有的、遗忘你所没有的

利用SWT做Java版局域网QQ(一)——基于UDP协议 – Huang

相关文章:

你感兴趣的文章:

标签云: