根據前兩篇博文,應該對插件開發有所了解。
前文回顧:
1 插件學習篇
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並不是通過掛載方式創建的,因此要獨立的釋放。
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 }