The Goal
          Keep walking……
          posts - 23,  comments - 1,  trackbacks - 0

          是JFace的類,繼承自ContributionManager,凡是繼承了IAction或IContribution接口的對(duì)象都可被加至ToolBarManager.你只要花時(shí)間為ToolBarManager添加Action,Toolbar和ToolItem實(shí)例會(huì)自動(dòng)產(chǎn)生。

          你可通過(guò)調(diào)用ApplicationWindow的createToolBarManager()來(lái)為你的應(yīng)用程序添加一個(gè)toolbar。與MenuManager不同的是,createToolBarManager()需要一個(gè)style參數(shù),這個(gè)參數(shù)用來(lái)設(shè)定ToolBar所用的按鈕的風(fēng)格:flat或normal。

          • ControlContribution

          除了MenuManager所用的ContributionItems之外,還有一個(gè)新的ContributionItem,只能被ToolBarManager使用——ControlContribution。這個(gè)類可將任何能被用于toolbar的Control打包進(jìn)去。

          要使用ControlContribution類,必須要實(shí)現(xiàn)抽象方法createControl().

          toolBarManager.add(new ControlContribution("Custom") {
          protected Control createControl(Composite parent) {
          SashForm sf = new SashForm(parent, SWT.NONE);
          Button b1 = new Button(sf, SWT.PUSH);
          b1.setText("Hello");
          Button b2 = new Button(sf, SWT.PUSH);
          b2.setText("World");
          b2.addSelectionListener(new SelectionAdapter() {
          public void widgetSelected(SelectionEvent e) {
          System.out.println("Selected:" + e);
          }
          });
          return sf;
          }
          });

          如果你希望有任何的事件發(fā)生,必須在你的controls上實(shí)現(xiàn)SelectionListeners

          • Creating toolbars by hand

          如果你不想ToolBarManager來(lái)創(chuàng)建toolbar的話,可以手動(dòng)創(chuàng)建,需要用到ToolBar和ToolItem類.

          Toolbar
          是一個(gè)composite control,包含多個(gè)ToolItems.Toolbar由多個(gè)小圖標(biāo)按鈕組成,一般是16-by-16bitmap圖片。每個(gè)按鈕都對(duì)應(yīng)一個(gè)ToolItem。Toolbar可以是水平的也可以是垂直的,默認(rèn)為水平

          ToolItem
          每一個(gè)ToolItem都有一個(gè)圖片,如果沒有,默認(rèn)為紅色方塊。When the user selects a ToolItem from the menu, it broadcasts the event to any registered SelectionListeners.Your application should register a listener with each ToolItem and use that listener to perform whatever logic corresponds to the menu item.

          posted @ 2006-04-07 16:32 JOO 閱讀(673) | 評(píng)論 (0)編輯 收藏

          三種類型的Combo control:
          1.Simple:默認(rèn)類型,一個(gè)可編輯的text field和一個(gè)供選擇的list
          2.Drop-down:下拉列表,文本框可編輯
          3.Read-only:文本框不可編輯的下拉列表,可用select( 0 )來(lái)將其默認(rèn)選中列表中的首項(xiàng)。

          以上三種類型可在構(gòu)造函數(shù)中通過(guò)STYLE.*來(lái)設(shè)置。

          posted @ 2006-04-07 16:30 JOO 閱讀(278) | 評(píng)論 (0)編輯 收藏

          先要將org.eclipse.text_x.y.z和org.eclipse.jface.text_x.y.z加到classpath

          • TextViewer and Document

          兩個(gè)重要的接口:IDocument和ITextViewer。JFace為其提供了默認(rèn)的實(shí)現(xiàn)。

          一個(gè)IDocument的實(shí)例持有被編輯的真實(shí)的文本信息。它的主要實(shí)現(xiàn)是Document類。AbstractDocument提供了部分實(shí)現(xiàn),你可通過(guò)繼承它來(lái)添加自己的實(shí)現(xiàn)。IDocument允許通過(guò)IDocumentListener接口來(lái)獲取內(nèi)容編輯的通知。

          IDocument還提供了以下功能
          Positions
          可以給每一個(gè)text區(qū)域分配一個(gè)記號(hào)來(lái)作為它的Position。當(dāng)被指定給某個(gè)ducument時(shí)一個(gè)Position對(duì)象有an offset and a length of text。如果document的text被更新的話,Position也會(huì)同步更新,所以他永遠(yuǎn)都是指向同一段文字。Position類本身提供了一些基本的功能,可通過(guò)繼承他來(lái)完善更多有用的功能。

          Partition content types
          每個(gè)document由一個(gè)或多個(gè)partitions組成,通過(guò)ITypedRegion接口來(lái)表現(xiàn)。每一個(gè)partition可以有各自的內(nèi)容類型,如plain text, rich text, or HTML。要使用它,你要?jiǎng)?chuàng)建一個(gè)IDocumentPartitioner然后assign給你的document,然后document的partitioner就會(huì)負(fù)責(zé)響應(yīng)對(duì)指定位置內(nèi)容類型的查詢,它必須通過(guò)實(shí)現(xiàn)computePartitioning()來(lái)返回包含此document中所有ITypedRegions的一個(gè)數(shù)組。不需要實(shí)現(xiàn)你自己的document partitioner。如果沒有創(chuàng)建,整個(gè)document就是一個(gè)區(qū)域,類型為IDocument.DEFAULT_CONTENT_TYPE。

          Searching
          IDocument通過(guò)search()提供了搜索的功能。不支持regular expressions or other patterns,但提供了search start location,direction, and case sensitivity and whether to match whole words only.

          ITextViewer將一個(gè)標(biāo)準(zhǔn)的text widget轉(zhuǎn)換成一個(gè)基于document的text widget

          ITextViewer的默認(rèn)實(shí)現(xiàn)是TextViewer,它使用StyledText來(lái)顯示數(shù)據(jù)。ITextViewer支持text modifications的listener,也支持visual events(如改變viewport,即text的當(dāng)前可視區(qū)域)的監(jiān)聽器。

          雖然作為ITextViewer的默認(rèn)應(yīng)用,如果你想要修改顯示,TextViewer允許你直接accessStyledText,但建議你使用TextPresentation,因?yàn)樗梢允占撐臋n中帶有的各個(gè)不同的StyleRanges。

          ITextViewer還支持很多不同類型的插件,可用來(lái)修改widget的行為。可以被customized的功能有:
          1.通過(guò)IUndoManager來(lái)支持undo
          2.通過(guò)ITextDoubleClickStrategy來(lái)支持對(duì)鼠標(biāo)雙擊的處理
          3.通過(guò)IAutoIndentStrategy來(lái)支持文本的自動(dòng)縮進(jìn)
          4.通過(guò)ITextHover來(lái)實(shí)現(xiàn),當(dāng)鼠標(biāo)停留在document的一個(gè)section上時(shí),顯示text.
          要使用上述插件,你需要分配一個(gè)適當(dāng)?shù)慕涌趯?shí)例給text viewer,然后調(diào)用activatePlugins().

          如下列出了org.eclipse.jface.text的子包及其作用

          posted @ 2006-04-04 15:02 JOO 閱讀(422) | 評(píng)論 (0)編輯 收藏

          用于文本編輯的control有2個(gè):Text和StyledText.后者可以為文本和control本身設(shè)定顏色,格式等。這兩個(gè)control之間毫無(wú)關(guān)聯(lián),除了都是Composite的子類之外。

          • The basic Text widget

          package com.swtjface.Ch5;

          import org.eclipse.swt.SWT;

          import org.eclipse.swt.events.VerifyEvent;

          import org.eclipse.swt.events.VerifyListener;

          import org.eclipse.swt.layout.FillLayout;

          import org.eclipse.swt.widgets.Composite;

          import org.eclipse.swt.widgets.Text;

          public class Ch5Capitalizer extends Composite {

          public Ch5Capitalizer(Composite parent) {

          super(parent, SWT.NONE);

          buildControls();

          }

          private void buildControls() {

          this.setLayout(new FillLayout());

          Text text = new Text(this, SWT.MULTI | SWT.V_SCROLL);

          text.addVerifyListener(new VerifyListener() { //每當(dāng)text被改變,任何以注冊(cè)的VerifyListeners便會(huì)被調(diào)用。此處每按一次鍵盤,此方法就被調(diào)用。如果是同時(shí)輸入多個(gè)字符,也調(diào)用一次

          public void verifyText(VerifyEvent e) {

          if( e.text.startsWith("1") ) {

          e.doit = false;

          } //如果文本以1開頭,即不允許編輯

          else {

          e.text = e.text.toUpperCase();

          }

          }

          });

          }

          }

          Text的重要方法,見下圖

          insert()--doesn’t allow you to insert text into the existing content.

          • The StyledText widget

          StyledText包含了一系列的應(yīng)用到該小部件的預(yù)定義的動(dòng)作,這些是常規(guī)的東西如:剪切、粘貼、移動(dòng)至下一個(gè)詞、移動(dòng)至文末。代表這些動(dòng)作的常量在org.eclipse.swt.custom程序包中的ST類中有定義。這些常量在兩種情況下發(fā)揮功效:首先,你可以使用它們程序性地使用invokeAction()方法調(diào)用任一的這些方法;其次,你也可以使用setKeyBinding()方法來(lái)將它們綁定于鍵擊行為。setKeyBinding()選定一個(gè)鍵(可以通過(guò)諸如Shift或是Ctrl之類的編輯鍵來(lái)修改SWT常量之一)綁定于指定的動(dòng)作。如下的例子中組合鍵Ctrl-Q綁定于粘貼動(dòng)作。引起注意的是這并不意味著會(huì)將默認(rèn)鍵的綁定清除,該兩個(gè)綁定都會(huì)生效。

          相對(duì)于Text而言,還添加了drawing line backgrounds and line styles的事件,可以通過(guò)此事件來(lái)改變整行的style或背景顏色。注意:如果使用了LineStyleListener,就不能在StyledText實(shí)例上調(diào)用get/setStyleRange(), 如果使用了LineBackgroundListener,那你就不能調(diào)用getLineBackground() or setLineBackground().

          可以通過(guò)使用一個(gè)StyledText的StyleRanges來(lái)改變顯示的風(fēng)格

          StyleRange
          StyledText通過(guò)使用StyleRange類來(lái)管理當(dāng)前所顯示的不同styles。其所有的欄位都是public的可隨意修改,但是要一直到當(dāng)此StyledText實(shí)例的setStyleRange()被調(diào)用之后才會(huì)生效。

          StyleRanges通過(guò)開始偏移量和長(zhǎng)度來(lái)設(shè)定text的區(qū)域范圍。
          StyleRange可設(shè)定背景和前景色,默認(rèn)為null,還可設(shè)定字體,SWT.NORMAL 或者SWT.BOLD.

          similarTo()可用來(lái)判斷兩個(gè)StyleRange實(shí)例是否有同樣的前景、背景和字體。

          當(dāng)我們保存text之后,可通過(guò)styledText.getStyleRanges()來(lái)獲取style信息,此函數(shù)會(huì)返回an array of StyleRange


          toggleBold()--將已輸入的文本在bold和normal之間切換,是被一個(gè)KeyListener調(diào)用的,此KeyListener會(huì)監(jiān)聽F1是否被按下

          A StyledText example
          復(fù)制、粘貼功能不需要通過(guò)代碼便可使用,是和platform的標(biāo)準(zhǔn)鍵盤快捷方式相關(guān)聯(lián)的

          ExtendedModifyListener和ModifyListener不同,前者提供了關(guān)于what was done的event細(xì)節(jié),而后者只是當(dāng)編輯懂作產(chǎn)生時(shí)notify,不會(huì)去準(zhǔn)確的辨別到底何種修改發(fā)生了。

          package com.swtjface.Ch5;

          import java.util.LinkedList;

          import java.util.List;

          import org.eclipse.swt.SWT;

          import org.eclipse.swt.custom.*;

          import org.eclipse.swt.events.KeyAdapter;

          import org.eclipse.swt.events.KeyEvent;

          import org.eclipse.swt.layout.FillLayout;

          import org.eclipse.swt.widgets.Composite;

          public class Ch5Undoable extends Composite {

          private static final int MAX_STACK_SIZE = 25;

          private List undoStack;

          private List redoStack;

          private StyledText styledText;

          public Ch5Undoable(Composite parent) {

          super(parent, SWT.NONE);

          undoStack = new LinkedList();

          redoStack = new LinkedList();

          buildControls();

          }

          private void buildControls() {

          this.setLayout(new FillLayout());

          styledText = new StyledText(this, SWT.MULTI | SWT.V_SCROLL);

          styledText.addExtendedModifyListener(

          new ExtendedModifyListener() { //每次text被編輯的時(shí)候,都會(huì)調(diào)用此listener

          public void modifyText(ExtendedModifyEvent event) {

          String currText = styledText.getText();

          String newText = currText.substring(event.start,

          event.start + event.length); //獲得新插入的文本

          if( newText != null && newText.length() > 0 ) {

          if( undoStack.size() == MAX_STACK_SIZE ) {

          undoStack.remove( undoStack.size() - 1 );

          }

          undoStack.add(0, newText);//將新插入的文本保存到undoStack中

          }

          }

          }); //關(guān)鍵部分

          styledText.addKeyListener(new KeyAdapter() {

          public void keyPressed(KeyEvent e) {

          switch(e.keyCode) {

          case SWT.F1:

          undo(); break;

          case SWT.F2:

          redo(); break;

          default: //ignore everything else

          }

          }

          });

          }

          private void undo() {

          if( undoStack.size() > 0 ) {

          String lastEdit = (String)undoStack.remove(0);//得到要undo的字符

          int editLength = lastEdit.length();

          String currText = styledText.getText();

          int startReplaceIndex = currText.length() - editLength;

          styledText.replaceTextRange(startReplaceIndex, editLength, ""); //將最后輸入的字符替換成空

          redoStack.add(0, lastEdit);//把最后的這個(gè)undo的字符加到redoStack中

          }

          }

          private void redo() {

          if( redoStack.size() > 0 ) {

          String text = (String)redoStack.remove(0);//得到要恢復(fù)的字符

          moveCursorToEnd();

          styledText.append(text);//將要恢復(fù)的字符加至文本的最后

          moveCursorToEnd();

          }

          }

          private void moveCursorToEnd() {

          styledText.setCaretOffset(styledText.getText().length());

          }

          }

          posted @ 2006-03-29 17:42 JOO 閱讀(1004) | 評(píng)論 (0)編輯 收藏

          ActionContributionItem--combines the function of a GUI widget and its attached listener class.
          Action--處理事件
          與SWT的listener/event模式很類似,但是其class更抽象,更易于使用,scope更窄。

          • actions and contributions

          Action--可以簡(jiǎn)單的理解成一個(gè)命令,可以關(guān)聯(lián)到菜單,工具條,以及按鈕
          Contribution--在JFace里面,一個(gè)Action可以對(duì)應(yīng)多個(gè)GUI對(duì)象,這些對(duì)象就是所謂的Contribution Item. 有兩個(gè)主要的Contribution類:ContributionItem和ContributionManager,它們都是抽象類,靠其子類來(lái)實(shí)現(xiàn)事件的處理。繼承關(guān)系見下圖
          ContributionItem--引發(fā)事件的單獨(dú)GUI組件
          ContributionManager--產(chǎn)生包含ContributionItems的對(duì)象

          ActionContributionItem--最重要,在ApplicationWindow中創(chuàng)建和實(shí)施,來(lái)將一個(gè)action連接至此GUI,它雖沒有設(shè)定好的外觀,但是依賴于你使用的fill()方法,卻可以幫助一個(gè)按鈕、菜單欄和工具欄的成形

          另一個(gè)與Contribution協(xié)作的方法是通過(guò)ContributionManager,它的子類類似于ContributionItem的container。其中MenuManager將ContributionItems組合在窗口最高層菜單, ToolBarManager則將這些對(duì)象放在僅在菜單之下的toolbar中。

          • 創(chuàng)建Action類

          Action是抽象類。

          package com.swtjface.Ch4;
          import org.eclipse.jface.action.*;
          import org.eclipse.jface.resource.*;
          public class Ch4_StatusAction extends Action
          {
          StatusLineManager statman;
          short triggercount = 0;
          public Ch4_StatusAction(StatusLineManager sm)
          {
          super("&Trigger@Ctrl+T",
          AS_PUSH_BUTTON);//在T字母之前的&符號(hào)意味著這個(gè)字母將作為該動(dòng)作的快捷鍵。而在TEXT領(lǐng)域內(nèi)的“Ctrl+T”確保了當(dāng)用戶在同時(shí)按下Ctrl鍵和T鍵時(shí)該動(dòng)作就會(huì)被激發(fā)。
          statman = sm;
          setToolTipText("Trigger the Action");
          setImageDescriptor(ImageDescriptor.createFromFile
          (this.getClass(),"eclipse.gif"));
          }
          public void run() //每次當(dāng)Ch4_StatusAction被生成,run()方法就被調(diào)用
          {
          triggercount++;
          statman.setMessage("The status action has fired. Count: " +
          triggercount);
          }

          • Implementing contributions in an ApplicationWindow

          package com.swtjface.Ch4;

          import org.eclipse.swt.*;

          import org.eclipse.swt.widgets.*;

          import org.eclipse.jface.window.*;

          import org.eclipse.jface.action.*;

          public class Ch4_Contributions extends ApplicationWindow {

          StatusLineManager slm = new StatusLineManager();

          Ch4_StatusAction status_action = new Ch4_StatusAction(slm); //StatusLineManager的對(duì)象作參數(shù),創(chuàng)建了一個(gè)Ch4_StatusAction的實(shí)例

          ActionContributionItem aci = new ActionContributionItem(status_action); //用Ch4_StatusAction的對(duì)象作參數(shù),創(chuàng)建了ActionContributionItem對(duì)象

          public Ch4_Contributions() {

          super(null); // 創(chuàng)建了 ApplicationWindow對(duì)象

          addStatusLine();

          addMenuBar();

          addToolBar(SWT.FLAT | SWT.WRAP); //在窗口上添加了status line, menu, toolbar

          }

          protected Control createContents(Composite parent) {

          getShell().setText("Action/Contribution Example");

          parent.setSize(290,150); //設(shè)置了窗口的title和size

          aci.fill(parent); // 將ActionContributionItem放在GUI中。因?yàn)檫@里的參數(shù)是Composite對(duì)象,所以根據(jù)Action的STYLE屬性來(lái)確定。此處是Button,因?yàn)?font color="#0000ff">Ch4_StatusAction 的STYLE屬性是AS_PUSH_BUTTON;

          return parent;

          }

          public static void main(String[] args) {

          Ch4_Contributions swin = new Ch4_Contributions();

          swin.setBlockOnOpen(true);

          swin.open();

          Display.getCurrent().dispose();

          }

          protected MenuManager createMenuManager() {

          MenuManager main_menu = new MenuManager(null);

          MenuManager action_menu = new MenuManager("Menu");

          main_menu.add(action_menu);

          action_menu.add(status_action); //關(guān)聯(lián)status_action.created and added to the menu in the form of a menu item

          return main_menu;

          }

          protected ToolBarManager createToolBarManager(int style) {

          ToolBarManager tool_bar_manager = new ToolBarManager(style);

          tool_bar_manager.add(status_action); //關(guān)聯(lián)status_action。created and added to the toolbar as a toolbar item.

          return tool_bar_manager;

          }

          protected StatusLineManager createStatusLineManager() {

          return slm;

          }

          }

          • Interfacing with contributions

          兩個(gè)途徑來(lái)將ActionContributionItem添加到GUI:
          1. 通過(guò)ContributionManager子類的add()方法。
          (1)可接受Action對(duì)象的參數(shù),從而間接的將ContributionItem和ContributionManager關(guān)聯(lián)。可多次執(zhí)行
          (2)可直接接受ActionContributionItem對(duì)象的參數(shù)。只可執(zhí)行一次
          2.通過(guò)ActionContributionItem類的fill()方法。根據(jù)其參數(shù)的不同,所先是的組件也不同,具體見下表:

          • Exploring the Action class

          Important methods of the Action class


          Property methods for the Action class

          DESCRIPTION--written to a status line to provide additional help.

          Style methods for the Action class

          如果ENABLED是FALSE,則變灰。CHECKED主要用于radio和checkbox

          Accelerator key / keyboard methods for the Action class

          Accelerator keys--鼠標(biāo)點(diǎn)擊的鍵盤塊捷方式


          Listener methods for the Action class

          雖然JFace使用action代替了SWT的listener/event機(jī)制,但是Actiono類仍然可以和listener協(xié)作來(lái)處理特定需求的事件。
          IPropertyChangeListener接口關(guān)注客戶自定義的PropertyChangeEvents,當(dāng)所給的對(duì)象按照你所給的方式變成另一個(gè)對(duì)象時(shí),此事件被觸發(fā)。

          Miscellaneous methods of the Action class

          posted @ 2006-03-24 17:02 JOO 閱讀(803) | 評(píng)論 (1)編輯 收藏
          • The SWT event-processing cycle


          1. Once an SWT application begins running, its Display class sorts through this queue using its readAndDispatch() method and msg field, which acts as a handle to the underlying OS message queue.
          2. If it finds anything relevant, it sends the event to its top-level Shell object, which determines which widget should receive the event.
          3. The Shell then sends the event to the widget that the user acted on, which transfers this information to an associated interface called a listener.
          4. One of the listener’s methods performs the necessary processing or invokes another method to handle the user’s action, called an event handler.


          所在包:org.eclipse.swt.events ??

          • typed listeners and events

          typed listeners--只對(duì)某一類的用戶事件起作用,繼承TypedListener類
          typed events--與此類特定動(dòng)作相關(guān)的事件,繼承TypedEvent類

          可通過(guò)add...Listener()method with the typed listener as the argument來(lái)將listener附加到widget。

          完整的typed events和listeners列表,如下:
          ?

          TypedEvent類包含了一些member field,他們提供與事件發(fā)生相關(guān)的一些信息,這些信息可以在event handler中使用來(lái)獲得與環(huán)境相關(guān)的信息。下圖為繼承自TypedEvent及EventObject類的fields:

          除此之外,很多的event類還有其他用來(lái)提供更多用戶動(dòng)作信息的fields,如MouseEvent類的button field

          要將listener加入到code中,有兩個(gè)主要的方法:
          第一個(gè)是在component的add...Listener()中創(chuàng)建一個(gè)匿名接口,這樣可以使listener的作用域僅限于此component,示例代碼如下:
          Button button = new Button(shell, SWT.PUSH | SWT.CENTER);//創(chuàng)建了一個(gè)button,并將其加入到shell中
          button.addMouseListener(new MouseListener() //創(chuàng)建了一個(gè)匿名MouseListener接口,并將其與button關(guān)聯(lián)
          {
          public void mouseDown(MouseEvent e)
          {
          clkdwnEventHandler();
          }
          public void mouseUp(MouseEvent e)
          {
          clkupEventHandler();
          }
          public void mouseDoubleClick(MouseEvent e)
          {
          dblclkEventHandler(); //此接口中必須被實(shí)現(xiàn)的三個(gè)方法。一旦鼠標(biāo)按下,放開或雙擊,就會(huì)有一個(gè)MouseEvent被發(fā)送到這三個(gè)方法中的一個(gè),然后,此方法再調(diào)用相關(guān)聯(lián)的event-handling方法(即下文中的三個(gè))。
          }
          });
          static void dblclkEventHandler()
          {
          System.out.println("Double click.");
          }
          static void clkdwnEventHandler()
          {
          System.out.println("Click - down.");
          }
          static void clkupEventHandler()
          {
          System.out.println("Click - up.");//event-handlers通過(guò)發(fā)送message到console來(lái)完成事件處理
          }

          上一類方法的缺點(diǎn)是此listener僅限于此component內(nèi),而第二種方法便可解決這種問題--獨(dú)立的聲明一個(gè)繼承MouseListener的接口,示例代碼如下:
          Button button = new Button(shell, SWT.PUSH | SWT.CENTER);
          button.addMouseListener(ExampleMouseListener);
          MouseListener ExampleMouseListener = new MouseListener()
          {
          public void mouseDoubleClick(MouseEvent e)
          {
          System.out.println("Double click.");
          }
          public void mouseDown(MouseEvent e)
          {
          System.out.println("Click - down.");
          }
          public void mouseUp(MouseEvent e)
          {
          System.out.println("Click - up.");
          }
          };

          使用MouseListener的缺點(diǎn)就是哪怕你只關(guān)心鼠標(biāo)雙擊事件,卻仍要聲明其接口中所包含的所有方法。
          ?

          • Adapter

          Adapter是繼承了Listener接口并提供了所有required方法的實(shí)現(xiàn)的abstract類。也就是說(shuō)如果你使用adapter而不是listener的話,你只需要寫你感興趣的方法

          只有那些listener有多個(gè)成員方法的event才有adapter,其完整列表見下圖:

          adapter同樣是通過(guò)add...Listener()方法來(lái)創(chuàng)建的,與listener類似,同樣也可通過(guò)匿名類和本地類兩種方法,下例為匿名類方法:
          button.addMouseListener(new MouseAdapter()
          {
          public void mouseDoubleClick(MouseEvent e)
          {
          dblclkEventHandler();
          }
          )};
          static void dblclkEventHandler()
          {
          System.out.println("Double click.");
          }

          • Keyboard events

          任何時(shí)候只要key被按下,就會(huì)創(chuàng)建KeyEvent,它有兩個(gè)子類:TraverseEvent 和VerifyEvent.
          TraverseEvent--當(dāng)按下arrow key或tab key來(lái)focus on text widget時(shí)
          VerifyEvent--fires when the user enters text that the program needs to check before taking further action.

          除了繼承自TypedEvent和EventObject的field,KeyEvent還包括三個(gè)member field來(lái)提供那些與觸發(fā)事件的key相關(guān)的信息,具體如下:
          character--代表被按下key的char
          stateMask--Returns an integer representing the state of the keyboard modifier keys.By examining this integer, a program can determine whether any of the Alt, Ctrl, Shift, and Command keys are currently pressed.
          keyCode--Provides the SWT public constant corresponding to the typed key. KeyCode列表,見下圖:


          TraverseEvent中有兩個(gè)fields:
          1. doit--返回布爾值,若為真則允許在各個(gè)component間切換focus,若為假則不允許切換focus
          2. detail--It’s an integer that represents the identity of the key that caused the event. For example, if the user presses the Tab key to switch to a new component, the detail field will contain the SWT constant TRAVERSE_TAB_NEXT.

          每個(gè)類型的Control對(duì)于所給的traversal key都有不同的默認(rèn)behavior,如果設(shè)doit為true,則override了其默認(rèn)的設(shè)置。

          VerifyEvent的field:
          1.start和end--設(shè)定了輸入的范圍
          2.text--contains the input String under examination.
          3.doit--Having looked at the user’s text, you set the boolean doit field to allow (TRUE) or disallow (FALSE) the action.

          • untyped events

          更靈活,但不安全,不推薦使用。
          當(dāng)一個(gè)代表著非類型化監(jiān)聽器的監(jiān)聽器類和GUI的某一組件相聯(lián)系時(shí),它就能接受該組件所能發(fā)送的任一類事件。因此,你需要操控這由Event類代表的捕獲的全部事件,決定用戶執(zhí)行的那個(gè)動(dòng)作。然后,正確的事件處理方法就被調(diào)用。

          不是被包含在org.eclipse.swt.events包中,而是被包含在org.eclipse.swt.widgets包中。

          代碼示例如下:
          Listener listener = new Listener ()
          {
          public void handleEvent (Event event)
          {
          switch (event.type)
          {
          case SWT.KeyDown:
          if (event.character == 'b')
          System.out.println("Key"+event.character);
          break;
          case SWT.MouseDown:
          if (event.button == 3)
          System.out.println("Right click");
          break;
          case SWT.MouseDoubleClick:
          System.out.println("Double click");
          break;
          }
          }
          };
          Button button = new Button(shell, SWT.CENTER);
          button.addListener(SWT.KeyDown, listener);
          button.addListener(SWT.MouseDown, listener);
          button.addListener(SWT.MouseDoubleClick, listener);


          Event類包含了所有typed event中的所有field,此外還有一個(gè)type field,其所有的值列表如下:

          posted @ 2006-03-23 16:32 JOO 閱讀(823) | 評(píng)論 (0)編輯 收藏
          • Composite類

          getChildren()
          lists the children of a Composite as an array of Control objects.
          getLayout()、setLayout(Layout)
          處理layout對(duì)象
          getTabList()、setTabList(Control[])
          指定widgets在Composite中的Tab順序(按鍵盤tab鍵的切換順序)

          Composite類是Scrollable類的直接子類,這就是說(shuō)SWT/JFace中的所有Composite對(duì)象都有Scrollbars.所有的Scrollable對(duì)象都可使用如下方法:
          getClientArea()、 computeTrim(int, int,int, int)
          trim--Composite的不可編輯區(qū)域,如title bars, scrollbars, 或status bars等.
          client area--那些可編輯,使用的區(qū)域
          getClientArea()--Returns the available display area of a Scrollable object
          computeTrim(int,int,int,int)--Returns the necessary dimensions of the Composite for the desired client area
          getHorizontalBar()、getVerticalBar()
          Returns the horizontal/vertical ScrollBar object

          Control, Scrollable, and Composite的關(guān)系,如下圖
          ??

          • Groups

          Composite子類中最簡(jiǎn)單的一個(gè),自身不執(zhí)行任何動(dòng)作,僅僅是將一組子widgets用矩形邊框起來(lái),此邊框類似于之前提到的分隔符,同樣也提供SWT.SHADOW_IN, SWT.SHADOW_OUT, SWT.SHADOW_NONE風(fēng)格,還可以通過(guò)選擇SWT.SHADOW_ETCHED_IN or SWT.SHADOW_ETCHED_OUT來(lái)客制化陰影效果。

          可以通過(guò)setText()方法來(lái)設(shè)定它的label

          與許多Widgets子類一樣,Group類不可被繼承。

          • SashForms

          可以在子widgets之間創(chuàng)建可移動(dòng)的分界,此分界稱為Sash.Sash類可以在org.eclipse.swt.widgets包中找到,而SashForms卻存在于org.eclipse.swt.custom包中。

          SWT.HORIZONTAL和SWT.VERTICAL--用來(lái)設(shè)定Sash的方向
          setOrientation()--用來(lái)設(shè)定Sash的方向
          getMaximizedControl()--returns the Control object that has been expanded the most.
          getWeights()--returns an int array containing the weight of each of the SashForm’s children.uses an int array to specify weights for each of the widgets in the Composite.

          • TabFolders

          creating and populating a TabFolder的四個(gè)步驟:
          1.?創(chuàng)建一個(gè)TabFolder實(shí)例
          2. 為每個(gè)頁(yè)面構(gòu)建一個(gè)TabItem對(duì)象
          3. 用setText()方法來(lái)設(shè)定tab的label
          4. setControl()--關(guān)聯(lián)一個(gè)control,當(dāng)它的tab被選中時(shí),顯示此control

          TabFolder類提供了一些用來(lái)獲得TabItems信息的方法,如下:
          getItemCount()
          返回此TabFolder所包含的TabItems數(shù)
          getItems()
          返回TabItems對(duì)象的數(shù)組
          getSelection()
          確定user選擇了哪個(gè)TabItem
          setSelection()
          Makes this decision from within the application

          posted @ 2006-03-22 17:55 JOO 閱讀(347) | 評(píng)論 (0)編輯 收藏
          • SWT.PUSH

          Button類的默認(rèn)風(fēng)格。
          SWT.FLAT

          • SWT.ARROW

          與SWT.UP, SWT.DOWN, SWT.LEFT, and SWT.RIGHT組合,也可使用SWT.FLAT

          • SWT.TOGGLE

          類似于SWT.PUSH,但是按下后會(huì)保持按下狀態(tài)。可通過(guò)setSelection(boolean)方法來(lái)改變其狀態(tài)。接下去介紹的check按鈕和radio按鈕也share這個(gè)功能

          • SWT.CHECK

          建議用數(shù)組來(lái)實(shí)現(xiàn),如下:
          Button[] checks = new Button[2];
          checks[0] = new Button(shell, SWT.CHECK);
          checks[0].setText("Choice 1");
          checks[0].setLocation(10,5);
          checks[0].pack();
          checks[1] = new Button(shell, SWT.CHECK);
          checks[1].setText("Choice 2");
          checks[1].setLocation(10,30);
          checks[1].pack();

          • SWT.RADIO

          建議用數(shù)組來(lái)實(shí)現(xiàn),如下:
          Button[] radios = new Button[3];
          radios[0] = new Button(shell, SWT.RADIO);
          radios[0].setSelected(true);
          radios[0].setText("Choice 1");
          radios[0].setLocation(10,5);
          radios[0].pack();
          radios[1] = new Button(shell, SWT.RADIO);
          radios[1].setText("Choice 2");
          radios[1].setLocation(10,30);
          radios[1].pack();
          radios[2] = new Button(shell, SWT.RADIO);
          radios[2].setText("Choice 3");
          radios[2].setLocation(10,55);
          radios[2].pack();
          for (int i=0; i<radios.length; i++)
          if (radios[i].getSelected())
          System.out.println(i);


          通過(guò)RadioGroupFieldEditors將radio buttons組合在一起:

          RadioGroupFieldEditor rgfe = new RadioGroupFieldEditor(
          "UserChoice", "Choose an option:", 1,
          new String[][] {{"Choice1", "ch1"},
          {"Choice2", "ch2"},
          {"Choice3", "ch3"}},
          shell, true);

          其中的各個(gè)參數(shù)含義如下——1.name for the type of value returned by the editor.2.group label.3.列數(shù)。4.creates a set of option names with their associated values.In this manner, the RadioGroupFieldEditor can display a series of radio buttons without allocating Button objects.5.將editor加至Shell對(duì)象。6.specifies whether the radio buttons should be incorporated in a Group object.

          在SWT/JFace中,container widgets是由Composite類來(lái)提供的。

          posted @ 2006-03-21 17:03 JOO 閱讀(285) | 評(píng)論 (0)編輯 收藏

          用于在GUI中顯示靜態(tài)的信息:String或Image

          • 風(fēng)格和分隔符

          對(duì)齊方式:SWT.CENTER,SWT.LEFT, and SWT.RIGHT.
          分隔符:SWT.SEPARATOR;SWT.VERTICAL, SWT.HORIZONTAL;SWT.SHADOW_IN, SWT.SHADOW_OUT, and SWT.SHADOW_NONE.

          • Label的方法

          posted @ 2006-03-21 16:32 JOO 閱讀(213) | 評(píng)論 (0)編輯 收藏
          • Widget類

          是所有widget的父類。為抽象類,而且Eclipse.org也強(qiáng)烈反對(duì)去繼承它,因此你既不能繼承他,也不能直接使用它
          Widget類中的重要方法,如下:

          • Control objects

          Control類中的對(duì)象在OS中有一個(gè)直接的副本,你可以通過(guò)類的handle field來(lái)直接access.然而SWT還提供了一些在Control類之外的widgets,如下所示:
          ?

          Control類的兩類方法:


          posted @ 2006-03-21 15:11 JOO 閱讀(239) | 評(píng)論 (0)編輯 收藏
          僅列出標(biāo)題
          共3頁(yè): 上一頁(yè) 1 2 3 下一頁(yè) 
          Hit the target!

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(2)

          隨筆分類(23)

          隨筆檔案(22)

          文章檔案(1)

          相冊(cè)

          Neighbor

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 繁昌县| 禄丰县| 屯门区| 富裕县| 祁阳县| 琼结县| 丽江市| 常山县| 定远县| 同德县| 卢湾区| 磐石市| 扶余县| 巴林右旗| 崇左市| 唐河县| 吉水县| 繁昌县| 朝阳市| 三原县| 金阳县| 长宁区| 通渭县| 崇仁县| 岑巩县| 长沙市| 基隆市| 忻城县| 锡林郭勒盟| 余姚市| 屯昌县| 临西县| 新建县| 晴隆县| 九台市| 太谷县| 新巴尔虎右旗| 定安县| 扶绥县| 什邡市| 毕节市|