官网:http://www.eclipse.org/swt/
开发说明:http://www.eclipse.org/swt/eclipse.php
1. 到官网上下载swt.zip文件,导入workspace
2. 选择路径
3. 在需要添加swt的工程中,properties的Java Build Path页包含org.eclipse.swt
4. 依赖org.eclipse.swt工程在自己的工程中使用swt
SWT实例
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.Display; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class HelloSWT extends Shell{ private static Text text; private static Button swtButton; private static Button swingButton; private static Button awtButton; private static Group group; private static Button button; private static Label benefitOfSwtLabel; private static List list; public static void main(String[] args){ /* * 创建display对象,一个SWT程序至少需要一个Display对象,创建线程称为UI线程 * 一个线程中不能同时有两个活动的Display存在 */ Display display = Display.getDefault(); /* * 一个Shell实例代表一个窗口 */ final Shell shell = new Shell(display);//创建窗口对象 shell.setText("Hello SWT"); shell.setSize(260,283);//设置窗口尺寸,以像素为单位 shell.open();//打开窗口,将窗口显示在屏幕上 text = new Text(shell, SWT.BORDER); text.setText("SWT是Eclipse平台使用的图形工具箱"); text.setBounds(10, 8, 230, 35); list = new List(shell, SWT.BORDER); list.setItems(new String[]{ "使用操作系统本地控件", "提供一套平台无关的API", "GUI程序的运行速度快", "更多更多......"}); list.setBounds(10, 68, 232, 82); benefitOfSwtLabel = new Label(shell, SWT.BORDER); benefitOfSwtLabel.setText("SWT的优点:"); benefitOfSwtLabel.setBounds(10, 49, 90, 20); group = new Group(shell, SWT.NONE); group.setText("你是用过哪些图形工具箱?"); group.setBounds(10, 159, 230, 47); awtButton = new Button(group, SWT.CHECK); awtButton.setText("AWT"); awtButton.setBounds(10, 20, 54, 18); swingButton = new Button(group, SWT.CHECK); swingButton.setText("Swing"); swingButton.setBounds(70, 22, 60, 15); swtButton = new Button(group, SWT.CHECK); swtButton.setText("SWT"); swtButton.setBounds(136, 22, 62, 15); button = new Button(shell, SWT.NONE); button.addSelectionListener(new SelectionAdapter(){ public void widgetSelected(final SelectionEvent e){ MessageBox msgBox = new MessageBox(shell, SWT.ICON_INFORMATION); msgBox.setMessage("Hello SWT!"); msgBox.open(); } }); button.setText("按一下按钮,向SWT说Hello"); button.setBounds(10, 214, 227, 25); shell.layout();//窗口布局 //事件循环,display的事件循环同时处理系统队列和自定义队列 while(!shell.isDisposed()){//若shell资源未被释放,事件循环一直继续 /* * 首先从事件队列中将事件取出来,经过必要的翻译后(TranslateMessage), * 若事件队列中读到事件,就将它发送到窗口处理(DispatchMessage); * 若在线程交互的事件队列中有需要执行的事件,就去执行它。 * 无事件处理,则使线程休眠,有新事件需要处理时唤醒线程 */ if(!display.readAndDispatch()){ display.sleep(); } } //释放占用的资源,所有它管理的shell都会被同时释放,(释放父资源时子资源同时被释放) display.dispose(); } }
结果: