包名称 package cn.iamsese.swt.study ;
定义一个类来做运行时的入口,这样可以减少main方法的使用,使自己更能进入SWT的学习
/** * 运行控制器类 */ public class Runner { public static void main(String[] args) { SimpleShell simpleShell = new SimpleShell(ExampleClassName.DefaultWindow); //SimpleShell simpleShell = new SimpleShell(ExampleClassName.ChildShell); } }
定义一个类用于分辨不同的demo例子,使得可以传入不同的构造函数到同一的构造函数处,以后没建立一个新实例,就在其中添加一个常数,使用16进制,因为好标识
/** * 实例类的名字 -- 通过在构造函数中调用不同的参数值来运行不同的实例程序 * cn.iamsese.swt.study * Author: vb2005xu [JAVA菜鸟] */ interface ExampleClassName { public final static int DefaultWindow = 0x1 ; //缺省实例 public final static int ChildShell = 0x2 ; //子窗口实例 }
导入的包 -- 以后按需要会添加
import org.eclipse.swt.* ; import org.eclipse.swt.widgets.*;
每添加一个例子,就在其中添加一些代码,后面会陆续跟新一些模板代码,但是大体东西不变
/** * 所有例子的外壳代码 * cn.iamsese.swt.study * Author: vb2005xu [JAVA菜鸟] */ class SimpleShell { private Display display ; private Shell shell ; public SimpleShell(String title){ this.display = new Display(); this.shell = new Shell(this.display); this.shell.setText(title); this.shell.setSize(500,500); SimpleShellUtil.setShellAtCenter(this.shell); } public SimpleShell(int className){ this("第一个SWT例子: cn.iamsese.swt.study.SimpleShell"); this.shell.open(); switch (className) { case ExampleClassName.ChildShell: this.childShellTest(this.shell); break; default: break; } while(! this.shell.isDisposed()){ if ( ! this.display.readAndDispatch() ){ this.display.sleep(); } } this.display.dispose(); } public void childShellTest(Shell shell){ //非模态对话框 ChildShell child1 = new ChildShell(shell); //模式对话框 -- 只禁用父窗体 ChildShell child2 = new ChildShell(shell,SWT.DIALOG_TRIM|SWT.APPLICATION_MODAL); //模式对话框 -- 禁用系统窗体 ChildShell child3 = new ChildShell(shell,SWT.DIALOG_TRIM|SWT.SYSTEM_MODAL); } }
代码例子1 -- ChildShell 每个代码例子,基本都是自包含的
/** * 子 窗口实例 * cn.iamsese.swt.study * Author: vb2005xu [JAVA菜鸟] */ class ChildShell { public ChildShell(Shell parent) { Shell child = new Shell(parent); child.setText("第二个SWT例子: cn.iamsese.swt.study.ChildShell"); child.setSize(400,100); child.open(); } public ChildShell(Shell parent,int style) { Shell child = new Shell(parent,style); child.setText("第二个SWT例子: cn.iamsese.swt.study.ChildShell"); child.setSize(400,100); child.open(); } }