關於SWT常用組件(按鈕,復選框,單選框(Button類))


Button是SWT中最常用的組件.Button類的繼承關系圖:

 

Button類的構造方法是newe Button(Composite parent,int style)它有兩個參數:

第一個參數:是Button創建在哪個容器上.Composite是最常用的容器,而Shell是

Composite的子類.所以此參數也能接受Shell和任何繼承自Composite的子類.

第二個參數用來指定Button應用哪種式樣,SWT.NONE是保持Button組件的默認式樣.

式樣其實是一個常量,如SWT.NONE的值是0,查看SWT.class的源代碼可以發現很多這種常量.

 

SWT組件的式樣是比較有特點的.和new Button(Composite parent,int style)一樣,大都在

構造函數中使用式樣(style)來定義組件外觀形態.例如:Button就可以通過式樣的定義而成為

復選框,單選框,或者通過式樣來設置按鈕文字的排放和按鈕外觀.

如果要生成一個文字靠左的,深陷型的復選框,只要用符號"|"將各個式樣連起來即可.

如下所示:new Button(shell,SWT.LEFT|SWT.BORDER|SWT.CHECK)

 

 

Button是SWT中最常用的組件.而且使用也比較簡單.

Button類的式樣表

SWT組件的式樣是比較有特點的,和new Button(Composite arent,int style)一樣,大都在構造函數中使用式樣(style)來定義組件外觀形態,例如:Button就可以通過式樣的定義而成為復選框,單選框或者通過式樣來設置按鈕文字的排放和按鈕外觀.

如果要生成一個文字靠左的,深陷型的復選框,只要用符號"|"將各個樣式連起來,既可以了.

例如:new Button(shell,SWT.LEFT|SWT.BORDER|SWT.CHECK)

 

組件的常用方法

SWT/JFace中的每一個組件都有很多的同名的方法,這些同名方法在各個組件的作用和用法都是相同或者相似的,這就為我們節省了很多的學習精力.在此將一些常用的方法總結如下:

setText(String string)

  說明:設置組件的標簽文字.

  例子:button.setText("確定").

setToolTipText(String string)

  說明:設置鼠標停留在組件上時,出現的黃色提示條中的文字.

  例子:button.setToolTipText("單擊確定按鈕,結束設置")

setBounds(int x,int y,int width,int height)

  說明:設置組件的坐標位置和大小,(x軸坐標,y軸坐標,按鈕寬度,按鈕長度)

  例子:button.setBounds(45,45,56,13)

setEnabled(boolean enabled)

  說明:設置組件是否可用,false不可用,true(默認值)可用

  例子:button.setEnable(false)

setFont(Font font)

  說明:設置文字的字體

  例子:button.setFont(ResourceManager.getFont("",14,SWT.BOLD|SWT.ITALIC)

setSelection(boolean selected)

  說明:設置是否選上,true為選上,false(默認)為不選上.當Button是復選框或者單選框的時候此方法才有效

  例子:button.setSelection(false)

setForeground(Color color)

  說明:設置前景色

  例子:button.setForeground()

setBackground(Color color)

  說明:設置背景色

  例子:label.setBackground()

setAlignment(int alignment)

  說明:設置標簽文字的對齊方式

  例子:label.setAlignment(SWT.RIGHT)

setImage(Image image)

  說明:設置顯示的圖片.

  例子:button.setImage(new Image(display,"icons/selectAll.gif"))  把圖片selectAll.gif設置在按鈕上.

 

例子代碼:

Button1.java (注意這個地方Button是一個類名,關鍵字,這個地方不能用Button.java命名類名,否則出現沖突)

 1 public class Button1 {
 2     public static void main(String[] args) {
 3 
 4         Display display = Display.getDefault();
 5         Shell shell = new Shell();
 6         shell.setSize(450, 300);
 7         shell.setText("SWT Application");
 8         
 9         //事件代碼里要訪問button,所以加上一個final
10         final Button btnNewButton = new Button(shell,SWT.NONE); 11         btnNewButton.addSelectionListener(new SelectionAdapter() {
12             @Override
13             public void widgetSelected(SelectionEvent e) {
14                 MessageDialog.openInformation(null, "","你單擊了 "+ btnNewButton.getText()+" 按鈕"); 15             }
16         });
17         btnNewButton.setBounds(78, 51, 80, 27);
18         btnNewButton.setText("確定"); 19 
20         shell.open();
21         shell.layout();
22         while (!shell.isDisposed()) {
23             if (!display.readAndDispatch()) {
24                 display.sleep();
25             }
26         }
27     }
28 }

運行結果:

程序說明:

Button類的造成方法是new Button(Composite parent,int style),它有兩個參數:

第一個參數是指Button創建在哪一個容器上.Composite是最常用的容器,而Shell是Composite的子類,所以此參數也能接受Shell和任何繼承自Composite的類.

第二個參數用來指定Button應用哪種(或幾種)式樣,SWT.NONE是保持Button組件的默認式樣.式樣其實是一個常量,如SWT.NONE的值是0,查看SWT.class的源代碼可以發現很多這種常量.

Button1.java

 

 1 public class Button1 {
 2     public static void main(String[] args) {
 3         final Display display = Display.getDefault();
 4         final Shell shell = new Shell();
 5         shell.setSize(327, 253);
 6         // ---------創建窗口中的其他界面組件-------------
 7         final Button radio1 = new Button(shell, SWT.RADIO);// 事件代碼里要訪問radio1,所以加一個final
 8         radio1.setText("男");// 設置按鈕上的文字
 9         radio1.setSelection(true);// 設置按鈕處於選擇狀態
10         radio1.setBounds(10, 10, 40, 25); // 設置按鈕位置
11 
12         final Button radio2 = new Button(shell, SWT.RADIO);
13         radio2.setText("女");
14         radio2.setBounds(10, 30, 40, 25);
15 
16         final Button check1 = new Button(shell, SWT.CHECK);
17         check1.setText("旅游");
18         check1.setBounds(70, 10, 40, 25);
19 
20         final Button check2 = new Button(shell, SWT.CHECK);
21         check2.setText("籃球");
22         check2.setBounds(70, 30, 40, 25);
23 
24         Button okButton = new Button(shell, SWT.NONE);
25         okButton.setText("確定");
26         okButton.setBounds(10, 70, 100, 25);
27         okButton.addSelectionListener(new SelectionAdapter() {
28             public void widgetSelected(SelectionEvent e) {
29                 String str = "小明,";
30                 if (radio1.getSelection())// 判斷按鈕是否被選
31                     str += radio1.getText();// 取得按鈕上的文字
32                 if (radio2.getSelection())
33                     str += radio2.getText();
34                 str += "。愛好:";
35                 if (check1.getSelection())
36                     str += check1.getText();
37                 if (check2.getSelection())
38                     str += check2.getText();
39                 // 信息提示框的第一、二個參數為空值也是可以的
40                 MessageDialog.openInformation(null, null, str);
41             }
42         });
43         // -----------------END------------------------
44         shell.layout();
45         shell.open();
46         while (!shell.isDisposed()) {
47             if (!display.readAndDispatch())
48                 display.sleep();
49         }
50         display.dispose();
51     }
52 }

 

 

程序說明:

Button類的構造方法是new Button(Composite parent,int style),它有兩個參數:

第一個參數指定Button創建在哪個容器上.Composite是最常用的容器,而Shell是Composite的子類,所以此參數也能接受Shell.

第二個參數指定Button所用的式樣:SWT.RaDIO 是單選按鈕;SWT.CHECK是復選框,SWT.NONE是普通按鈕(默認式樣)

 

Button類的式樣表.

SWT組件的創建格式和Button一樣,一般都是構造函數的第一個參數指定父容器,第二個參數指定式樣,不過各組件所支持的式樣各有不同.SWT通過式樣來定義組件的外觀形態;

式樣其實是一個常量,SWT.NONE的對應值是0,查看SWT.class的源代碼能發現很多這種常量.多個式樣可以用符號"|"組合在一起,比如生成一個文字靠左的,深陷型的復選框:new Button(shell,SWT.LEFT|SWT.BORDER|SWT.CHECK).

 

使用事件參數SelectionEvent

事件方法一般都帶有一個參數,比如Button的選擇事件widgetSelected(SelectionEvent e)的參數SelectionEvent.事件參數一般會包含一些事件觸發者(如Button)的信息.

舉個實例:一窗口中0~9依次排列着十個數字按鈕,單擊這些按鈕后彈出提示框顯示相應的數值,實現這個例子的關鍵是獲得數字按鈕對象的引用,有兩種方式,第一種是慣用做法:

Button2.java

 1 public class Button2 {
 2     public static void main(String[] args) {
 3         final Display display = Display.getDefault();
 4         final Shell shell = new Shell();
 5         shell.setSize(327, 253);
 6         // ---------創建窗口中的其他界面組件-------------
 7         for (int i = 0; i < 10; i++) {
 8             final Button button = new Button(shell, SWT.NONE);
 9             button.setText(i + "");
10             button.setBounds(i * 20, 10, 20, 20); // 設置按鈕位置
11             button.addSelectionListener(new SelectionAdapter() {
12                 public void widgetSelected(SelectionEvent e) {
13                     MessageDialog.openInformation(null, null, button.getText());
14                 }
15             });
16         }
17         // -----------------END------------------------
18         shell.layout();
19         shell.open();
20         while (!shell.isDisposed()) {
21             if (!display.readAndDispatch())
22                 display.sleep();
23         }
24         display.dispose();
25     }
26 }

第二種是通過事件參數SelectionEvent的getSource方法得到按鈕對象的引用,另外,由於事件代碼中不必直接引用button變量,所以第二句前面不用加final前綴.

Button3.java

 1 public class Button3 {
 2     public static void main(String[] args) {
 3         final Display display = Display.getDefault();
 4         final Shell shell = new Shell();
 5         shell.setSize(327, 253);
 6         // ---------創建窗口中的其他界面組件-------------
 7         for (int i = 0; i < 10; i++) {
 8             Button button = new Button(shell, SWT.NONE);
 9             button.setText(i + "");
10             button.setBounds(i * 20, 10, 20, 20); // 設置按鈕位置
11             button.addSelectionListener(new SelectionAdapter() {
12                 public void widgetSelected(SelectionEvent e) {
13                     Button b = (Button) e.getSource();
14                     MessageDialog.openInformation(null, null, b.getText());
15                 }
16             });
17         }
18         // -----------------END------------------------
19         shell.layout();
20         shell.open();
21         while (!shell.isDisposed()) {
22             if (!display.readAndDispatch())
23                 display.sleep();
24         }
25         display.dispose();
26     }
27 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM