• admin
  • 384
  • 2025-10-31 05:32:31

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

前文回顾:

1 插件学习篇

2 简单的建立插件工程以及模型文件分析

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

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

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

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

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

最常用的还要数Control了,因为很多Button Label控件都是继承这个类,在开发中经常使用的方法就是

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并不是通过挂载方式创建的,因此要独立的释放。

代码语言:javascript代码运行次数:0运行复制 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就可以进行窗口的编辑了。

代码语言:javascript代码运行次数:0运行复制 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 }