kooyee ‘s blog

          開源軟件, 眾人努力的結(jié)晶, 全人類的共同財富
          posts - 103, comments - 55, trackbacks - 0, articles - 66
             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
          一個button類的實例 


          基本的步驟和之前的是一樣的,一個SWT的項目,然后在頁面設(shè)置面板里添加button。雙擊button(直接在source里面添加了監(jiān)聽器的代碼)。下面是代碼:

          public class ButtonTest {
              
          public static void main(String[] args) {
                  
          final Display display = Display.getDefault();
                  
          final Shell shell = new Shell();
                  shell.setSize(
          500375);
                  shell.setText(
          "SWT Application");
                  shell.open();

                  
          final Button okButton = new Button(shell, SWT.NONE);
                  
          //因為是比較簡單的例子,只在這一個地方用這個的原因,所以用匿名方式
                  okButton.addMouseListener(new MouseAdapter() {
                      
          public void mouseUp(MouseEvent e) {
                          MessageDialog.openInformation(
          null,"","you click the "+ okButton.getText() +"button");
                      }

                  }
          );
                  okButton.setText(
          "OK.");
                  okButton.setBounds(
          9719624454);
                  shell.layout();
                  
          while (!shell.isDisposed()) {
                      
          if (!display.readAndDispatch())
                          display.sleep();
                  }

              }

          }

          程序說明: Button類的構(gòu)造方法是new Button(Composite parent,int style),它有2個參數(shù):

          1. 第一個參數(shù)是指Button創(chuàng)建在哪個容器上,Composite是最常用的容器,而shell是Composite的子類,所以此參數(shù)也能接受shell和任何繼承Composite的類。
          2. 第二個參數(shù)用來指定button應(yīng)用那種(或幾種)式樣,SWT.NONE是保持Button組件的默認(rèn)式樣。式樣其實是一個常量,如SWT.NONE的值是0。等等....
          3. 讓button應(yīng)用多是式樣... 要生成一個文字靠左的,深陷型的復(fù)選框,只要用 “ | ” 將各個樣式連起來即可,如下所示:new Button(shell,SWT.LEFT|SWT.BORDER|SWT.CHECK)。
          •  組件的常用方法

          SWT/JFace中的每一個組件之間都有很多同名的方法,很幸運這些方法在各個組件里的作用和用法都是相同或相似的,常用方法簡述如下:

          okButton.setImage(SWTResourceManager.getImage(ButtonTest.class, "275.jpg"));
          okButton.setBackground(SWTResourceManager.getColor(158, 224, 167));
          okButton.setText("OK.");
          okButton.setBounds(81, 183, 244, 54);
          等很多的方法,到現(xiàn)在的SWT編輯器下面這些常用的方法都可以在頁面設(shè)置項里找到,所以很方便。

          • 一個文本框(text類)的實例

          本實例中創(chuàng)建一個文本框,它有如下功能:

          1. 只能輸入數(shù)字。
          2. 至少輸入一個值。
          3. 長度不能多于10個字符。    (實例代碼如下:)
            public class ButtonTest {
                
            private static Text text;
                
            public static void main(String[] args) {
                    
            final Display display = Display.getDefault();
                    
            final Shell shell = new Shell();
                    shell.setSize(
            500375);
                    shell.setText(
            "SWT Application");
                    shell.open();

                    
            final Button okButton = new Button(shell, SWT.NONE);
                    okButton.setImage(SWTResourceManager.getImage(ButtonTest.
            class"275.jpg"));
                    okButton.addSelectionListener(
            new SelectionAdapter() {
                        
            public void widgetSelected(SelectionEvent e) {
                            String str 
            = text.getText();
                            
            if (str == null || str.equals("")) {
                                MessageDialog.openInformation(shell, 
            """please enter a number");
                            }
             else {
                                MessageDialog.openInformation(shell, 
            """your enter the number is true");
                            }

                        }

                    }
            );
                    okButton.setText(
            "OK.");
                    okButton.setBounds(
            8118324454);

                    text 
            = new Text(shell, SWT.BORDER);
                    text.setTextLimit(
            10);  //最都只能輸入10個字符
                    text.setBounds(708522846);
                    text.addVerifyListener(
            new VerifyListener(){//檢查監(jiān)聽器,每輸入一個字符都回觸發(fā)
                        public void verifyText(VerifyEvent e) {
                            
            //檢查輸入字符(e.text)是否在0123456789這個字符串中,不在indexOf會返回-1
                            boolean b = "0123456789".indexOf(e.text) >= 0 ;
                            e.doit 
            = b;  //doit屬性如果為true,則字符允許輸入,反之不允許
                        }

                    }
            );

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

                }

            }

          注意到這次MessageDialog的第一個參數(shù)用了shell做參數(shù),而不是以前的null。兩者的區(qū)別在于:用shell時,則彈出提示窗口時,windows任務(wù)欄不會新增一個任務(wù)項;用null時,則多出一個任務(wù)項。

          常用方法:

          1. setEchoChar(char echo)
            說明:將輸入到文本框的字符表示成echo字符.
            例子:text.setEchoChar('*');相當(dāng)于SWT.PASSWORD式樣。
          2. setTabs(int tabs)
            說明:按Tab鍵時前進(jìn)多少個空格的長度,默認(rèn)值為8.只有當(dāng)text的樣式為SWT.MULTI,SWT.V_SCROLL,SWT.H_SCROLL時,此設(shè)置才會有效。
            例子:text.setTabs(4) 按鍵時前進(jìn)4個空格長度
          3.  setTopIndex(int index)
            說明:轉(zhuǎn)到文本框的第index行,0為第一行,此命令可以在文本框中進(jìn)行快速行定位。

            例子:text.setTopIndex(0); 將當(dāng)前行定位到首行上。
             
          4. setTextLimit(int limit)
            說明:設(shè)置最大入力數(shù)。
            例子:text.setTextLimit(10); 文本框最多只能輸入10個字符。
             

          • 一個下拉框(combo類)的實例

           

          public class ComboTest {
              
          private static Combo combo;
              
          public static void main(String[] args) {
                  
          final Display display = Display.getDefault();
                  
          final Shell shell = new Shell();
                  shell.setSize(
          500375);
                  shell.setText(
          "SWT Application");
                  shell.open();
                  combo 
          = new Combo(shell, SWT.READ_ONLY);//定義一個只讀的下拉框
                  combo.setBounds(846730859);

                  
          final Button button = new Button(shell, SWT.NONE);
                  button.setText(
          "input key");
                  button.setBounds(
          5422013552);        
                  button.addSelectionListener(
          new SelectionAdapter() {
                      
          public void widgetSelected(SelectionEvent e) {
                          combo.removeAll(); 
          //先清空combo
                          for(int i=1;i<10;i++)
                              combo.add(i
          +"");//在combo中顯示 1 到 9 
                          combo.select(0);    //設(shè)置 當(dāng)前項是第一項
                      }

                  }
          );

                  
          final Button button_1 = new Button(shell, SWT.NONE);
                  button_1.setText(
          "get key");
                  button_1.setBounds(
          27622012452);        
                  button_1.addSelectionListener(
          new SelectionAdapter() {
                      
          public void widgetSelected(SelectionEvent e) {
                          MessageDialog.openInformation(shell, 
          null, combo.getText());
                      }

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

              }

          }

           程序說明:
          在這個例子中最關(guān)鍵的是設(shè)置值部分的代碼,它是用add添加的,還有一種combo類設(shè)置值的方法,是將所有的值存到一個字符串?dāng)?shù)組,然后將這個數(shù)組作為參數(shù),批量加入到combo中, 語句如下:
          combo.setItems(new String[]{"1","2","3"});

          讓combo的各項和對象一一對應(yīng)
                  combo中的項只能是字符串,但是在實際應(yīng)用中有很多這樣的需要:將combo中的各項和一些對象一一對應(yīng)起來。這些對象可以是任何類,這時就可以根據(jù)選擇各項來得到相應(yīng)的對象。
                  要實現(xiàn)這樣的功能,使用setData方法就可以了。基于上面的例子,只需要修改設(shè)值和取值的事件代碼。代碼如下:

                ... ...  
                
          final Button button = new Button(shell, SWT.NONE);
                  button.addSelectionListener(
          new SelectionAdapter() {
                      
          public void widgetSelected(SelectionEvent e) {
                          combo.removeAll();
                          
          for(int i=1;i<10;i++){
                              combo.add(i
          +"no used");//這時的這行代碼的作用就僅僅是在頁面上顯示的標(biāo)簽                    
                              combo.setData(""+(i-1), new Integer(i)); //關(guān)鍵代碼,具體說明看下面的【代碼說明】                    
                          }

                          combo.select(
          0);
                      }

                  }
          );
                  ... ...
                  
          final Button button_1 = new Button(shell, SWT.NONE);
                  button_1.addSelectionListener(
          new SelectionAdapter() {
                      
          public void widgetSelected(SelectionEvent e) {
                          String key 
          = "" + combo.getSelectionIndex(); //取得key
                          Integer selectObject = (Integer)combo.getData(key);//通過key 取得object               
                          
          //下面是顯示結(jié)果
                          MessageDialog.openInformation(shell, null"select the Integer is "+ selectObject.toString());
                      }

                  }
          );
                 ... ...

           程序說明:
          setData方法的格式是:setData(key,Object),它將對象(Object)對應(yīng)一個鍵值(key),然后附著在combo上,鍵值可以是任意字符串,上例中之所以用""+(i-1),是因為它的值正好和combo的SelectionIndex值(當(dāng)前項的序號)相同,這樣在取值時就方便了。另外SelectionIndex的起始值是0,所以要 i -1 ( i 循環(huán)起始值是1) 。
          combo中的樣式 SWT.SIMPLE(無須單擊下拉框,列表一直顯示)

          常用方法除了上面用到的,還有combo.deselectAll();就是讓當(dāng)前的選擇項為空,就是不選擇任何項。
          select( int  index); 將index+1項設(shè)置為當(dāng)前選擇項。 combo.select(0);使首項為選擇項。
          setText(String string) 此方法和上面的一樣也是設(shè)置當(dāng)前選擇項的。區(qū)別是上面是通過Index來選擇的,這個是通過combo里面的String的內(nèi)容來選擇的,例如:  combo.setText("中國"); 就是把中國的那項選擇為當(dāng)前選擇的,另外setText沒有選擇的背景陰影,select則有。
           

          • 列表框(List類)

              list的用法和combo基本一致,combo的兩個例子只要將Text定義部分改為list的定義語句,再稍微修改,即可成為list類的例子。
              在list中設(shè)值的方法和combo是一樣的,但取值有所不同,這時因為list可以選擇多項,而combo只能選擇一項,combo'的getText可以得到當(dāng)前選擇項的字符串,而list由于可以選擇多項,所以就不行了。
              list取值用getSelection()方法,它返回的是一個所有選項組成的String數(shù)組。


          評論

          # re: eclipse學(xué)習(xí)筆記!(4) ----- SWT Designer 下 SWT常用組件   回復(fù)  更多評論   

          2008-05-22 13:51 by 求助
          很好很有用哈!

          # re: eclipse學(xué)習(xí)筆記!(4) ----- SWT Designer 下 SWT常用組件   回復(fù)  更多評論   

          2009-09-17 16:47 by fasfsafsa
          不錯,很有用.
          主站蜘蛛池模板: 青浦区| 宣化县| 龙陵县| 施甸县| 浦江县| 天镇县| 纳雍县| 集安市| 浑源县| 贵阳市| 井冈山市| 公主岭市| 天津市| 西乡县| 绍兴县| 长宁区| 吉水县| 鹤岗市| 夏河县| 富阳市| 太原市| 嘉兴市| 襄樊市| 昌都县| 定陶县| 双柏县| 嵊州市| 龙口市| 福泉市| 武城县| 通道| 乐都县| 贺兰县| 浪卡子县| 敦煌市| 北海市| 古浪县| 黄冈市| 龙川县| 涡阳县| 双辽市|