我們最常用的圖形構件基本都被包括在*.widgets包中。比如Button、Combo、Text、Label、Sash、Table等等。其中兩個最重要的構件當數Shell和Composite。
Shell相當於應用程序的主窗口框架。Composite相當於SWING中的Panel對象,充當構件容器的角色。當我們想在一個窗口中加入一些構件時,最好使用Composite作爲構件的容器,然後再去*.layout包找出一種合適的佈局方式。SWT對構件的佈局也採用了SWING或AWT中Layout和Layout Data結合的方式。在*.layout包中可以找到四種Layout和與它們相對應的佈局結構對象(Layout Data)。在*.custom包中,包含了對一些基本圖形構件的擴展。比如其中的CLabel,就是對標準Label構件的擴展,在它上面可以同時加入文字和圖片,也可以加邊框。StyledText是Text構件的擴展。它提供了豐富的文本功能,比如對某段文字的背景色、前景色或字體的設置。在*.custom包中也可找到一個新的StackLayout佈局方式。
SWT對用戶操作的響應,比如鼠標或鍵盤事件,也是採用了AWT和SWING中的Observer模式。在*.event包中可以找到事件監聽的Listener接口和相應的事件對象。例如常用的鼠標事件監聽接口MouseListener、MouseMoveListener和MouseTrackListener,及對應的事件對象MouseEvent。
*.graphics包中可以找到針對圖片、光標、字體或繪圖的API。比如可通過Image類調用系統中不同類型的圖片文件。通過GC類實現對圖片、構件或顯示器的繪圖功能。
Eclipse還針對不同的平臺開發了一些富有針對性的API。例如,在Windows平臺下,可以通過*.ole.win32包很容易地調用ole控件。這使Java程序內嵌IE瀏覽器、Word或Excel等程序成爲可能。
更復雜的程序
下面這個程序擁有一個文本框和一個按鍵,當用戶點擊按鍵的時候,文本框顯示一句歡迎信息。
爲了文本框和按鍵有比較合理的大小和佈局,這裏採用了GradLayout佈局方式。這種佈局是SWT中最常用也是最強大的佈局方式,幾乎所有的格式都可能通過GradLayout完成。下面的程序也涉及瞭如何應用系統資源(Color)以及如何釋放系統資源。
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class WelcomeDemo {
public WelcomeDemo() {
}
private void initShell(Shell shell) {
// 爲shell設置標題
shell.setText("WelcomeDemo");
// 爲shell設置佈局對象
GridLayout sLayout = new GridLayout();
shell.setLayout(sLayout);
// 構造一個Composite組件作爲文本框和按鈕的容器
Composite panel = new Composite(shell, SWT.NONE);
// 爲panel設置佈局結構對象
// 讓panel儘可能地佔滿shell
GridData pLayoutData = new
GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL
| GridData.FILL_BOTH);
panel.setLayoutData(pLayoutData);
// 爲panel設置佈局對象
// 文本框和按鈕將按這個佈局對象顯示
GridLayout pLayout = new GridLayout();
panel.setLayout(pLayout);
// 爲panel設置背景色
final Color pColor = new Color(Display.getCurrent(), 0, 0, 255);
panel.setBackground(pColor);
// 構造文本框
final Text text = new Text(panel, SWT.MULTI | SWT.WRAP);
// 爲文本框設置佈局結構對象
// 讓文本框儘可能地佔滿panel
GridData tLayoutData = new
GridData(SWT.FILL, SWT.FILL, true, true);
text.setLayoutData(tLayoutData);
// 構造按鈕
Button button = new Button(panel, SWT.PUSH);
button.setText("確定");
// 爲按鈕添加鼠標事件
button.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
// 當用戶點擊按鈕時顯示信息
text.setText("歡迎");
}
});
// 當主窗口關閉時觸發DisposeListener
shell.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
// 釋放panel背景色資源
pColor.dispose();
}
});
}
public static void main(String args[]) {
WelcomeDemo demo = new WelcomeDemo();
Display display = new Display();
Shell shell = new Shell(display);
demo.initShell(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
線程問題
Java語言本身提供了多線程機制,這種機制對GUI編程來說是不利的。它不能保證圖形組件操作的同步與串行化。SWT採用了一種簡單而直接的方式去適應本地GUI系統對線程的要求:在SWT中,通常存在一個被稱爲“用戶線程”的唯一線程。只有在這個線程中才能調用對組件或某些圖形API的訪問操作。如果在非用戶線程中程序直接調用這些訪問操作,那麼就會拋出SWTExcepiton異常。但是SWT也在*.widget.Display類中提供了兩個方法可以間接地在非用戶線程中進行圖形組件的訪問操作。這是通過syncExec(Runnable)和asyncExec(Runnable)這兩個方法實現的。例如:
// 此時程序運行在一個非用戶線程中,並且希望在組件panel上加入一個按鈕。
Display.getCurrent().asyncExec(new Runnable() {
public void run() {
Button button = new Button(panel, SWT.PUSH);
button.setText("Push");
}
});
方法syncExec()和asyncExec()的區別在於前者要在指定的線程執行結束後才返回,而後者則無論指定的線程是否執行都會立即返回到當前線程。
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/iffen/archive/2007/05/20/1618180.aspx