【插件开发】—— 4 SWT编程须知

  根据前两篇博文,应该对插件开发有所了解。html

前文回顾:java

插件学习篇shell

简单的创建插件工程以及模型文件分析框架

利用扩展点,开发透视图eclipse

  SWT知识介绍

  以前学过Java的朋友,多少页会一些关于Swing的东西。那么这里的SWT就是Eclipse插件所应用到的小部件开发框架。函数

  里面包含了大量的桌面控件,而且进行了一系列的优化整合,相对于Swing,极大的减小了内存的消耗。并且关于资源的释放也须要开发者注意,须要特定的手动删除,可是好比一个部件的子部件会随着该部件的销毁而销毁。学习

  下面看一下开发中经常使用的一些部件模型,这里介绍的并不全,小控件其实有不少不少,这里就简单的介绍几种:测试

  

  这里Widget是一个超类,全部的部件都继承与这个类。它也提供了一些经常使用的方法,好比添加一些监听,获取经常使用的信息等等。优化

  最经常使用的还要数Control了,由于不少Button Label控件都是继承这个类,在开发中常常使用的方法就是this

  addMouseListener()进行鼠标点击的监听

  setBounds 进行控件的从新绘制

  等等。具体的函数,你们能够经过开发多留意一下,就好了。

 

  关于SWT里面Display与Shell之间的关系

  Eclipse插件开发的程序大多有个不成文的规定,一个程序活动期间,只能有一个Dispaly对象,可是能够有多个Shell对象。那么,什么是Dispaly,什么又是Shell呢。

  

 

  这里红色箭头显示的就是一个Display,也就是一个底层的应用实例。若是这个实例没有被销毁,而程序意外中止了,那么是不能从新运行的。也就是说,运行期间,一个应用程序,只能有一个Display。就像显示器与窗口内的内容,只有一个显示器,可是显示器内部能够显示多个文件内容。

  绿色箭头对应的就是Shell,一个Shell至关于一个活动的窗口,能够在里面添加各类小部件,组成一个丰富的应用界面。

  综上,一个Display能够有多个Shell,可是只有一个Display(适用于普通状况).!

 

  在Main中启动开发界面

  接下来介绍一下如何不启动一个Eclipse 插件工程,来开发SWT。这个过程不少教材上都有描述,所以这里只提供了上面例子所对应的代码。

  要注意的是,最后要释放资源,Shell是挂载到Dispaly上面(原谅我用挂载这个词,Linux里面挂载比较生动),所以销毁Display的时候,能够自动的销毁Shell对象。可是Color并非经过挂载方式建立的,所以要独立的释放。

 1 package com.xingoo.plugin.swttest;  2 
 3 import javax.swing.Scrollable;  4 import javax.swing.text.StyleConstants.ColorConstants;  5 
 6 import org.eclipse.swt.SWT;  7 import org.eclipse.swt.graphics.Color;  8 import org.eclipse.swt.layout.FillLayout;  9 import org.eclipse.swt.widgets.Display; 10 import org.eclipse.swt.widgets.Label; 11 import org.eclipse.swt.widgets.Shell; 12 import org.eclipse.swt.widgets.Text; 13 
14 public class mainTestExample { 15     public static void main(String[] args) { 16         Display display = new Display(); 17         Color color =  new Color(display,255,0,0); 18         
19         //create a shell
20         Shell shell_1 = new Shell(display); 21         shell_1.setText("This is a shell in main function()"); 22         shell_1.setBounds(100,100,400,200); 23         shell_1.setLayout(new FillLayout()); 24         
25         Label label_1 = new Label(shell_1,SWT.CENTER); 26         label_1.setText("this is the text of a label"); 27  label_1.setForeground(color); 28         
29  shell_1.open(); 30  Text test; 31         //create another shell
32         Shell shell_2 = new Shell(display); 33         shell_2.setText("This is a shell1 in main function()"); 34         shell_2.setBounds(250,250,400,200); 35         shell_2.setLayout(new FillLayout()); 36         
37         Label label_2 = new Label(shell_2,SWT.CENTER); 38         label_2.setText("this is the text of a label1"); 39  label_2.setForeground(color); 40         
41  shell_2.open(); 42         
43         while(!shell_1.isDisposed() || !shell_2.isDisposed()){ 44             if(!display.readAndDispatch()) 45  display.sleep(); 46  } 47         
48         //dispose the resource
49  display.beep(); 50  color.dispose(); 51  display.dispose(); 52  } 53 }

  这个函数代码在通常 工程 里面就能够运行,可是缺乏一个Jar包,swt的jar包,这个jar包在Eclipse的plugins文件夹下就能够找到。能够经过引入的方式,引入到工程中。

  其实只须要swtx86这个jar包就能够了,source是源代码,可让我跟踪调试swt的源码。

便于继承的窗口抽象类

  为了后面的测试使用,这里能够把这段代码进行提取。这样以后的main函数的类只要继承这个AbstractExample就能够进行窗口的编辑了。

 1 package com.xingoo.plugin.swttest;  2 
 3 import org.eclipse.swt.SWT;  4 import org.eclipse.swt.layout.FillLayout;  5 import org.eclipse.swt.widgets.Display;  6 import org.eclipse.swt.widgets.Label;  7 import org.eclipse.swt.widgets.Shell;  8 
 9 abstract class AbstractExample{ 10     public void run(){ 11         Display display = new Display(); 12         Shell shell = new Shell(display); 13         shell.setText("shell example"); 14         shell.setBounds(100,100,400,200); 15         shell.setLayout(new FillLayout()); 16  todo(shell); 17  shell.open(); 18         
19         while(!shell.isDisposed()){ 20             if(!display.readAndDispatch()) 21  display.sleep(); 22  } 23         //dispose the resource
24  display.beep(); 25  display.dispose(); 26  } 27     public abstract void todo(Shell shell);//extension something here
28 } 29 
30 public class mainTestExample extends AbstractExample{ 31     public static void main(String[] args) { 32         new mainTestExample().run(); 33  } 34 
35     public void todo(Shell shell) { 36         //...add something you like
37         Label label_1 = new Label(shell,SWT.CENTER); 38         label_1.setText("this is the text of a label"); 39  } 40 }
相关文章
相关标签/搜索