[doc] 基本元件的使用

看板java作者 (隱者)時間19年前 (2006/04/22 07:27), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/2 (看更多)
Title: 基本元件的使用 Requirement: 了解SWT程式的基本架構 本文說所的「基本元件」僅為作者個人的講法,對於尚未使用SWT撰寫GUI 的人來說。能先在視窗中佈置一些元件有助於增長信心的。而這些基本元 件共用的特性為:僅以最簡單的操作形式就能使用。本文說要介紹的基本 元件有:Label、Text、Button、List、Combo。此外,還有將元件群組或 組裝功能的元件:Composite、Group。 下列程式碼為SWT 的骨架,以註解的方式標明元件合理的配置位置。每一 個SWT 程式,至少需要一個特殊的Display 類別,負責控制SWT 本身與作 業系統間的通訊,再以一個Shell 類別,持有真正的視窗。而元件則要佈 置在Shell 產生的視窗之上。因此,元件合理的產生位置。應該在Shell 實例產生之後至Shell 實例釋放之前。 ============================================================================== public class SWT { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); /* 元件配置區 */ shell.setSize(400, 300); shell.setText("SWT"); shell.open(); while (!shell.isDisposed()) if (!display.readAndDispatch()) { display.sleep(); } display.dispose(); } } ============================================================================== 在正式使用元件之前,先瀏覽一下物類的繼承關係: 以Button為例[1], java.lang.Object org.eclipse.swt.widgets.Widget org.eclipse.swt.widgets.Control org.eclipse.swt.widgets.Button 在Button與其抽象類別之間,尚有一個Control類別負責處理使用者與視 視窗之間互動的行為。像焦點(focus) 控制,註冊事件接收器等等。都依 賴Control類別來控制。而更上一層為所有元件的抽象類別Widget ,也是 規範元件產的主要介面。Widget類別的建構式只有一個,需指定一個父元 件(parent)以及此Widget類別的風格(style): Widget(Widget parent, int style) 本文所介紹的基本元件都繼承自Widget,所以配置元件都使用此建構式。 而建構式中parent參數應為何者呢?再次回顧SWT 程式基本架構。需先產 生一個溝通SWT 程式與系統底層的Display,再以此Display為一特殊的不 可視元件作為Shell 的父元件將真實的視窗呈現於畫面。而其他的元件可 以為Shell 的子元件。因此,目前所知,我們及將產生的元件,父元件可 指定為Shell。至於style則使用SWT 類別定義的常數來決定。可供指定的 style也依元件有所不同,可查閱API後再決定設定的內容。以Button類別 為例: 以下為Button類別的手冊部分內容,每一個元件皆有三個部分的主要描述 。Styles 說明該類別能使用的style;Events列出可供註冊的事件;Note 則附加說明Styles 使用的限制。由Note可看出,style大致分為二組,一 為指定按鈕型態的style:ARROW、CHECK、PUSH、RADIO與TOGGLE。一為指 定按鈕內文字對齊方式的:LEFT、RIGHT與CENTER 。此外,還附加說明, 當style為ARROW需指定ARROW的方向:UP、DOWN、LEFT與RIGHT。有這樣的 了解就能依類別說明自行查閱。 <% Styles: ARROW, CHECK, PUSH, RADIO, TOGGLE, FLAT UP, DOWN, LEFT, RIGHT, CENTER Events: Selection Note: Only one of the styles ARROW, CHECK, PUSH, RADIO, and TOGGLE may be specified. Note: Only one of the styles LEFT, RIGHT, and CENTER may be specified. Note: Only one of the styles UP, DOWN, LEFT, and RIGHT may be specified when the ARROW style is specified. IMPORTANT: This class is intended to be subclassed only within the SWT implementation. %> 看懂了手冊的描述後,就能試著產生Button 類別。如前文所述,style常 數定義於SWT內,並且皆為static 。所以,你可以用下列程式碼產生一個 Button。 <% Button b = new Button(shell, SWT.PUSH); %> 而若要同時指定,文字靠右對齊,可改寫為: <% Button b = new Button(shell, SWT.PUSH | SWT. RIGHT); %> 其他的元件:Label、Text、List、Combo。用法則與Button相似。最後, 提供一個完整可執行的範例: ========================================================================= import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ButtonEx { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); Button button = new Button(shell, SWT.PUSH); button.setText("I am a button"); /* 使用pack method將button調整至少需要的大小。 在不使用版面管理員的情況下, 若無設定大小,則元件會呈現於shell中的一個小點。 */ button.pack(); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } } ========================================================================= [1] 注意:非所有本文列舉的基本元件都是如此的繼承關係。 -- 我所追求的純粹只是任性,完全的任性。 -- 挪威的森林 -- ※ Origin: SayYA 資訊站 <bbs.sayya.org> ◆ From: pc210-59-94-118.nutn.edu.tw ◆ Modify: 05/08/17 9:04:01 <pc210-59-94-118.nutn.edu.tw> ※ X-Info: qrtt1 -> qrtt1.bbs@ptt.cc ※ X-Sign: 124IQHH0chkS2kH6BT2Y (06/04/22 7:26:41 ) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 210.59.94.148 ※ 編輯: qrtt1 來自: 210.59.94.148 (04/22 07:27)
文章代碼(AID): #14IMfaLD (java)
文章代碼(AID): #14IMfaLD (java)