标签:
eclipse插件开发用到图形界面的时候需要SWT,因此这里记录下开发环境然后写一个demo以便以后快速搭建工作环境:
关于开发环境,本人也看了几篇帖子,要安装个什么windowsbuilder,网址http://download.eclipse.org/windowbuilder/WB/release/R201309271200/3.7/
但是昨晚测试了一下,不知道是不是网络问题,没有能快速下载,然后找csdn下载了一个,选择archive安装,但是还是不好使,觉得没必要折腾,就找了个现成的,已经搭建好环境的eclipse3.5,传上去以后直接拿来用,
主要还是有个界面可以拖拽一下位置,

建议不要用它来拖出组件,因为会生成一个
private static final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
这样一来会和我下面的代码产生冲突,这里如果是SWT高手可能会对我进行批评,因为我的代码比较生硬,我确实没搞过SWT,所以仅仅是能用,没有考虑质量的问题,建议新手就像我这样来,避免不必要的麻烦:
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;
}
}
运行结果:

解释必要点:
SWT程序入口就是Display和Shell,所以一开始就创建好固定的写法,然后要什么按钮控件自己再往上添加即可。
标签:
原文地址:http://my.oschina.net/u/555061/blog/493123