隨筆-17  評(píng)論-64  文章-79  trackbacks-1
          1、Eclipse中swt的配置

            建議配置:jdk1.4.2以及eclipse3.1

            在代碼中調(diào)用swt控件之前,首先建立一個(gè)項(xiàng)目,然后選擇該項(xiàng)目的properties -> Java Build Path,將standard Widget ToolKit加入到Library頁(yè)當(dāng)中。

            接下來可以建立第一個(gè)eclipse小程序,新建一個(gè)class,并且在該class所對(duì)應(yīng)的代碼中輸入如下程序,其中package以及class名稱根據(jù)實(shí)際情況來確定名稱。

          package mypakage;
          import org.eclipse.swt.widgets.*;
          import org.eclipse.swt.*;
          /*導(dǎo)入需要的類庫(kù)*/
          public class Myfrm1 {
          public Myfrm1() {
          super();
          }
          public static void main(String[] args) {
          Display display = new Display();
          Shell shell = new Shell(display);
          /*shell為一個(gè)窗口對(duì)象*/
          Label label = new Label(shell, SWT.NONE);
          label.setText("Hello, World!"); /*創(chuàng)建一個(gè)標(biāo)簽對(duì)象并且設(shè)置標(biāo)題文字*/
          label.pack();
          shell.pack();
          shell.open(); /*打開并顯示窗口*/

          while(!shell.isDisposed())
          if(!display.readAndDispatch())
          display.sleep(); /*在窗口沒有銷毀之前,顯示對(duì)象一直處于等待狀態(tài)*/

          display.dispose(); /*否則,銷毀對(duì)象,釋放對(duì)象所占據(jù)的資源*/
          label.dispose();
          }
          }

            運(yùn)行上述代碼(run -> debug -> swt application)將產(chǎn)生如下所示的一個(gè)窗口

            2、button的使用

            按鈕可能的類型有很多,例如:

          SWT.BORDER 含有邊框的按鈕

          SWT.CHECK 復(fù)選按鈕

          SWT.PUSH 普通按鈕

          SWT.RADIO 單選按鈕

            3、Text的使用

            文本框的類型也有很多種選擇,例如:

          SWT.BORDER 含有邊框

          SWT.READ_ONLY 只讀

            下圖為包含按鈕以及文本框的窗口

            設(shè)計(jì)上述窗口所對(duì)應(yīng)的代碼為:

          package mypakage;
          import org.eclipse.swt.widgets.*;
          import org.eclipse.swt.SWT;
          import org.eclipse.swt.events.*;
          import org.eclipse.swt.layout.*;
          public class Myfrm1 {
          public Myfrm1() {
          super();
          }
          public static void main(String[] args) {
          Display display = new Display( );
          Shell shell = new Shell(display);
          shell.setSize(300, 200);
          shell.setLayout(new RowLayout( ));
          shell.setText("Button Example");
          final Button button = new Button(shell, SWT.BORDER);
          button.setText("Click Me");
          final Text text = new Text(shell, SWT.BORDER);
          shell.open( );

          while(!shell.isDisposed( )) {
          if(!display.readAndDispatch( )) display.sleep( );
          }
          display.dispose( );
          }
          }

            如果想對(duì)控件的位置以及大小進(jìn)行精確的設(shè)置,可以使用setBounds(x, y, width, height)方法來取代shell.setLayout(new RowLayout( ))。例如:button.setBounds(80, 80, 90, 20);

            button的監(jiān)聽及事件處理

            對(duì)按鈕單擊事件處理的代碼:

          button.addSelectionListener(new SelectionListener( )
          {
          public void widgetSelected(SelectionEvent event)
          {
          text.setText("No worries!");
          }
          public void widgetDefaultSelected(SelectionEvent event)

          {
          text.setText("No worries!");
          }
          });

            將以上代碼加入到shell.open之前,當(dāng)點(diǎn)擊按鈕時(shí)產(chǎn)生以下效果:


            分析:由于為button按鈕增加了一個(gè)監(jiān)聽器,按鈕時(shí)刻處于被“監(jiān)控”的狀態(tài),當(dāng)按鈕控件被選擇(點(diǎn)擊)既選擇事件發(fā)生時(shí),對(duì)文本控件進(jìn)行賦值”No worries”。

            根據(jù)監(jiān)聽事件的原理,設(shè)計(jì)如下程序,該程序能夠獲得鼠標(biāo)的X坐標(biāo),顯示在文本框中:

          package mypakage;
          import org.eclipse.swt.widgets.*;
          import org.eclipse.swt.SWT;
          import org.eclipse.swt.events.*;
          import org.eclipse.swt.layout.*;

          public class Myfrm1 {
          public Myfrm1() {
          super();
          }
          public static void main(String[] args) {
          Display display = new Display( );
          Shell shell = new Shell(display);
          shell.setSize(300, 200);
          shell.setLayout(new RowLayout( ));
          final Text text = new Text(shell, SWT.SHADOW_IN);

          shell.addMouseMoveListener(new MouseMoveListener( )
          {
          public void mouseMove(MouseEvent e)
          {
          Integer y=new Integer(e.x); /*將x坐標(biāo)轉(zhuǎn)換為Integer類型的對(duì)象*/
          text.setText(y.toString());
          }
          });

          shell.open( );
          while(!shell.isDisposed( )) {
          if(!display.readAndDispatch( )) display.sleep( );
          }
          display.dispose( );
          }
          }

            監(jiān)聽方式:

          ControlListener 用于處理移動(dòng)以及尺寸變化

          FocusListener 用于處理得到焦點(diǎn)以及失去焦點(diǎn)

          KeyListener 處理按鍵的輸入

          MouseListener , MouseMoveListener, MouseTrackListener 對(duì)鼠標(biāo)的動(dòng)作進(jìn)行處理

          SelectionListener 處理控件的選擇行為(包括按鈕的點(diǎn)擊)

            注意:監(jiān)聽方式與其所能夠處理的事件具有一定的關(guān)聯(lián)性,既監(jiān)聽方式?jīng)Q定了所能夠處理事件的種類,例如:

          shell.addMouseListener(new MouseListener( )
          {
          public void mouseMove(MouseEvent e)
          {text.setText("mousemove");}
          public void mouseDoubleClick(MouseEvent e)
          {text.setText("mousedbclc");}
          public void mouseDown(MouseEvent e)
          {}
          public void mouseUp(MouseEvent e)
          {}
          });

            你會(huì)發(fā)現(xiàn)在鼠標(biāo)移動(dòng)時(shí),text.setText("mousemove");始終不能夠執(zhí)行;并且mouseDown、mouseUp事件不能夠省略,原因就在于MouseListener只能處理mouseDoubleClick、mouseDown、mouseUp三類事件,而且這三類事件不能夠分離。

            3、List控件

            List控件的樣式包括:

          SWT.BORDER 含有邊框

          SWT.H_SCROLL 含有水平滾動(dòng)條

          SWT.V_SCROLL 含有垂直滾動(dòng)條

          SWT.SINGLE 允許單選

          SWT.MULTI 允許復(fù)選

            若要?jiǎng)?chuàng)建一個(gè)含有從11個(gè)元素的List,可以通過以下代碼來實(shí)現(xiàn)

          final List list = new List (shell, SWT.SINGLE);
          for (int i=0;i<=10;i++)
          list.add("item"+i);

            以下實(shí)例能夠判斷List控件中所選擇的選項(xiàng),并且輸出顯示在控制臺(tái)中:

          package mypakage;
          import org.eclipse.swt.widgets.*;
          import org.eclipse.swt.SWT;
          import org.eclipse.swt.events.*;
          import org.eclipse.swt.layout.*;
          public class Myfrm1 {
          public Myfrm1() {
          super();
          }
          public static void main(String[] args) {
          Display display = new Display ( );
          Shell shell = new Shell (display);
          shell.setText("List Example");
          shell.setSize(300, 200);
          shell.setLayout(new FillLayout(SWT.VERTICAL));
          final List list = new List (shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
          for (int loopIndex = 0; loopIndex < 100; loopIndex++){
          list.add("Item " + loopIndex);
          }
          list.addSelectionListener(new SelectionListener( )
          {
          public void widgetSelected(SelectionEvent event)
          {
          int selections[] = list.getSelectionIndices ( );
          String outText = "";
          for (int loopIndex = 0; loopIndex < selections.length;
          loopIndex++) outText += selections[loopIndex] + " ";
          System.out.println ("You selected: " + outText);
          }
          public void widgetDefaultSelected(SelectionEvent event)
          {
          int [] selections = list.getSelectionIndices ( );
          String outText = "";
          for (int loopIndex = 0; loopIndex < selections.length; loopIndex++)
          outText += selections[loopIndex] + " ";
          System.out.println ("You selected: " + outText);
          }
          });
          shell.open ( );
          while (!shell.isDisposed ( )) {
          if (!display.readAndDispatch ( )) display.sleep ( );
          }
          display.dispose ( );
          }
          }

            效果圖:

            You selected: 4 5 6 7 8 9 10

            分析:list.getSelectionIndices ( )方法將會(huì)獲得被選擇項(xiàng)目的集合, selections[]或者[] elections表示動(dòng)態(tài)一維數(shù)組。

            4、Menu控件

            建立菜單的一般步驟為:

            1、在建立菜單時(shí),首先需要建立一個(gè)菜單欄,需要使用SWT.BAR屬性

            Menu menuBar = new Menu(shell, SWT.BAR);

            2、在菜單欄的基礎(chǔ)之上,創(chuàng)建下拉菜單的所對(duì)應(yīng)的頂級(jí)菜單項(xiàng),需要使用SWT.CASCADE屬性
          fileMenuHeader = new MenuItem(menuBar, SWT.CASCADE);

            fileMenuHeader.setText("&File");

            3、建立與頂級(jí)菜單項(xiàng)相關(guān)的下拉式菜單

            dropMenu1 = new Menu(shell, SWT.DROP_DOWN);

            4、將頂級(jí)菜單項(xiàng)與下拉菜單關(guān)聯(lián)

          MenuHeader1.setMenu(dropMenu1);

            5、為下拉菜單添加子菜單項(xiàng)

          dropitem1= new MenuItem(dropMenu1, SWT.PUSH);
          dropitem1.setText("open");

            6、最后,在窗口中指定需要顯示的菜單欄

          shell.setMenuBar(menuBar);

            菜單的監(jiān)聽及事件

            參照按鈕的監(jiān)聽以及事件,設(shè)計(jì)如下程序,當(dāng)點(diǎn)擊 File子菜單下的“open”時(shí),在文本框中顯示“click open menu!”

          dropitem1.addSelectionListener(new SelectionListener()
          {
          public void widgetSelected(SelectionEvent event)
          {
          text.setText("click open menu!");
          }
          public void widgetDefaultSelected(SelectionEvent event)
          {
          text.setText("click open menu!");
          }
          });

            5、使用工具欄toobar

            建立工具欄可以通過如下方式:ToolBar toolbar = new ToolBar(shell, SWT.NONE);

            在工具欄的基礎(chǔ)之上創(chuàng)建工具欄子按鈕,并且設(shè)置子按鈕的標(biāo)題:

          ToolItem item1 = new ToolItem(toolbar, SWT.PUSH);
          item1.setText("item1");

            例如:

          ToolBar toolbar = new ToolBar(shell, SWT.NONE);
          ToolItem item1 = new ToolItem(toolbar, SWT.PUSH);
          item1.setText("item1");
          ToolItem item2 = new ToolItem(toolbar, SWT.PUSH);
          item2.setText("item2");

            工具欄的監(jiān)聽及事件

            實(shí)例:創(chuàng)建一個(gè)監(jiān)聽對(duì)象,將該監(jiān)聽對(duì)象應(yīng)用于每一個(gè)按鈕,最終來判斷鼠標(biāo)點(diǎn)擊的是哪一個(gè)按鈕,效果圖如下。

          Listener listener = new Listener( ) {
          public void handleEvent(Event event) {
          ToolItem item =(ToolItem)event.widget;
          String string = item.getText( );
          text.setText("You selected:" + string); }
          };
          item1.addListener(SWT.Selection, listener);
          item2.addListener(SWT.Selection, listener);
          item3.addListener(SWT.Selection, listener);
          item4.addListener(SWT.Selection, listener);

            6、滾動(dòng)條slider的使用

            滾動(dòng)條分為有邊框、垂直、水平三種類型,利用slider.setBounds方法可以指定滾動(dòng)條所在的位置。

            滾動(dòng)條所能夠處理事件的包括:

          SWT.ARROW_DOWN 向下或向右按鈕被點(diǎn)擊

          SWT.ARROW_UP 向左或向上按鈕被點(diǎn)擊

          SWT.DRAG 滑塊按鈕被托動(dòng)

          SWT.END 滑塊到達(dá)終點(diǎn)

          SWT.HOME 滑塊到達(dá)起點(diǎn)

          SWT.PAGE_DOWN 下方或右側(cè)的滾動(dòng)條被點(diǎn)擊

          SWT.PAGE_UP 上方或左側(cè)的滾動(dòng)條被點(diǎn)擊

            實(shí)例:根據(jù)滑塊的位置移動(dòng)按鈕位置

          slider.addListener(SWT.Selection, new Listener( ) {

          public void handleEvent(Event event) {

          switch(event.detail) {

          case SWT.ARROW_DOWN: button.setBounds(slider.getSelection(),0,20,10);

          break;

          case SWT.ARROW_UP:button.setBounds(slider.getSelection(),0,20,10);

          break;

          case SWT.DRAG:button.setBounds(slider.getSelection(),0,20,10);

          break;

          case SWT.END:button.setBounds(slider.getSelection(),0,20,10);

          break;

          case SWT.HOME:button.setBounds(slider.getSelection(),0,20,10);

          break;

          case SWT.PAGE_DOWN:button.setBounds(slider.getSelection(),0,20,10);

          break;

          case SWT.PAGE_UP:button.setBounds(slider.getSelection(),0,20,10);

          break;

          }
          }

          });

            7、樹形控件Tree

            樹形控件使用的方法為,首先創(chuàng)建一個(gè)Tree類型的對(duì)象,其次在該對(duì)象的基礎(chǔ)之上繼續(xù)擴(kuò)展節(jié)點(diǎn),以及擴(kuò)展節(jié)點(diǎn)的子節(jié)點(diǎn)。

            final Tree tree = new Tree(shell, SWT.BORDER);

            可以利用tree.setSize方法來改變樹形控件的大小。在創(chuàng)建節(jié)點(diǎn)時(shí),需要指明該節(jié)點(diǎn)所依賴的父節(jié)點(diǎn)的名稱,如TreeItem item0 = new TreeItem(tree, 0);,那么item0將成為tree對(duì)象中的0級(jí)(頂級(jí))節(jié)點(diǎn)。

            如下程序?qū)⒃趖ree對(duì)象的基礎(chǔ)之上產(chǎn)生9個(gè)節(jié)點(diǎn):

          final Tree tree = new Tree(shell, SWT.BORDER);

          tree.setSize(290, 290);

          for(int loopIndex1 = 2000; loopIndex1 <= 2008; loopIndex1++) {

          TreeItem item0 = new TreeItem(tree, 0);

          item0.setText("Year " + loopIndex1);

          }

            在上述實(shí)例的基礎(chǔ)上為每一個(gè)0級(jí)節(jié)點(diǎn)的基礎(chǔ)上擴(kuò)展出12個(gè)節(jié)點(diǎn):

          for(int loopIndex1 = 2000; loopIndex1 <= 2008; loopIndex1++) {

          TreeItem item0 = new TreeItem(tree, 0);

          item0.setText("Year " + loopIndex1);

          for(int loopIndex2 = 1; loopIndex2 <= 12; loopIndex2++) {

          TreeItem item1 = new TreeItem(item0, 0);

          item1.setText("Month " + loopIndex2);
          }

          }

            8、對(duì)話框dialog

            對(duì)話框是一個(gè)依托于主窗體的子窗體,如圖所示。

            例如:當(dāng)在主窗體中點(diǎn)擊按鈕時(shí),彈出一個(gè)對(duì)話框dialog,當(dāng)關(guān)閉對(duì)話框時(shí)按鈕顯示“dialog is disposed”

          Display display = new Display( );

          final Shell shell = new Shell(display);

          shell.setSize(300, 200);

          shell.setText("main");

          final Button opener = new Button(shell, SWT.PUSH);

          opener.setText("Click Me");

          opener.setBounds(20, 20, 50, 25);

          final Shell dialog = new Shell(shell, SWT.APPLICATION_MODAL |

          SWT.DIALOG_TRIM);

          dialog.setText("dialog");

          dialog.setBounds(10,10,50,60);

          dialog.addDisposeListener(new DisposeListener(){

          public void widgetDisposed(DisposeEvent e){

          opener.setText("dialog is disposed");

          }
          });

          Listener openerListener = new Listener( ) {

          public void handleEvent(Event event) {

          dialog.open( );

          }

          };

          opener.addListener(SWT.Selection, openerListener);

          shell.open( );

          while(!dialog.isDisposed( )) {

          if(!display.readAndDispatch( )) display.sleep( );

          }

          while (!shell.isDisposed( )) {

          if (!display.readAndDispatch( ))

          display.sleep( );

          }

          display.dispose( );

          posted on 2006-04-22 01:23 飛鳥 閱讀(635) 評(píng)論(3)  編輯  收藏 所屬分類: JAVA

          評(píng)論:
          # cheap cialis 2014-05-16 16:48 | cheap_cialis
          # cheap viagra 2014-05-16 16:48 | cheap_viagra
          # cheap cialis 2014-05-16 16:49 | cheap_cialis
          主站蜘蛛池模板: 家居| 四子王旗| 湟中县| 乌鲁木齐市| 商南县| 三门峡市| 邵阳市| 德昌县| 天门市| 项城市| 大庆市| 霍山县| 睢宁县| 时尚| 双鸭山市| 稷山县| 酒泉市| 五华县| 洪洞县| 白玉县| 朔州市| 临沧市| 阜平县| 遂溪县| 南木林县| 鄂托克前旗| 南开区| 肇州县| 东山县| 平塘县| 改则县| 黑山县| 玉环县| 兰考县| 潮安县| 茌平县| 婺源县| 尚志市| 卓资县| 玛多县| 沙坪坝区|