SWTJface学习笔记2(helloword)

    技术2022-05-11  66

      看来写个笔记还是有用的,以前看过的好多东西都找不到在哪里了。我学习swt/jace就是看例子,不懂的地方就去查资料或者问别人,所以我的笔记也会是每篇写几个例子,然后加上自己的理解,不对的地方还希望高手能够指出,大家互相学习吧^_^o(∩_∩)o..。

    下面就以一个最简单也是最常见的例子 helloword来开头吧(好像每个语言一开始都是这个)

    下面这个例子是摘自《The Definitive Guide to SWT and JFace 》这本书,我发现这本书上的例子都很好,就是这书没有中文版的,我的英文又烂,不过还好代码能看懂一些^_^。   入门的小例子
    import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell;   public class HelloWorld {     public static void main(String[] args) {         final Display display = Display.getDefault();         final Shell shell = new Shell();         shell.setSize(500, 375);         shell.setText("第一个SWT");         Label label = new Label(shell, SWT.CENTER);          label.setText("Hello, World");          label.setBounds(shell.getClientArea());             shell.open();         shell.layout();         while (!shell.isDisposed()) {             if (!display.readAndDispatch())                 display.sleep();         }     }   }  
    import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; 这个就不用说了吧。
      首先看这两行: final Display display = Display.getDefault();         final Shell shell = new Shell(); 生成一个 display和shell实例. Display的作用是负责将你的代码中的swt和jface命令翻译成底层命令来调取操作系统,是图形界面的风格和操作系统风格相似。Display()分派了平台资源并产生一个Display对象 Shell是程序的主窗口 下面看看这个: Label label = new Label(shell, SWT.CENTER);          label.setText("Hello, World");          label.setBounds(shell.getClientArea()); 创建一个 label组件 显示 Hello, World 最后看: shell.open();         shell.layout();         while (!shell.isDisposed()) {             if (!display.readAndDispatch())                 display.sleep(); } shell保持open状态,Display的实例就会使用readAndDispatch方法来追踪操作系统事件队列中与用户相关的事件,当某一个动作促使窗口关闭时,与Display对象(包括shell及其子部件等)相联系的资源就全部释放。
       
     上面这三段代码就代表了swt/jace程序的三个部分,如果你用的是eclipse+SWT-Designer那么第一部分和第三部分都是自动生成的,只有label是你自己定义的。 未完、、、、

    最新回复(0)