【插件開發】—— 5 SWT控件以及布局使用


 

前文回顧:

插件學習篇

簡單的建立插件工程以及模型文件分析

利用擴展點,開發透視圖

4 SWT編程須知

 

  經過前幾篇的介紹,多少對SWT又有了一些認識,那么這篇繼續來看一下一些控件的組合使用。

  首先是幾種簡單的控件,Label,Text,Button,Combo這些都是些常用的簡單框架,但是為了能夠構造出整齊的布局,還是要多花些心思的。

  除了這些簡單的控件外,還有點復雜的控件,比如Table和樹、選項卡和菜單等等,這里就先不做介紹了。

 

  為了整個這些控件,經常要使用兩個組合控件以及多種布局。

  1 【Group 組】,這個組可以為我們生成一個帶有線的框,這樣可以把雜亂的控件放到一個規整的容器內。

  2 【Composite 組合控件】,它是為了拼接一些簡單的控件,形成具有復雜功能的整合控件。

  比如文件路徑的瀏覽,往往就需要一個文件瀏覽的按鈕,和一個文本框。

 

  這里先放出一段代碼,代碼中使用到了簡單的布局模型GridLayout(),以及組和組合控件,還有一些簡單的控件。形成一個登陸界面,並且單擊按鈕可以出發響應事件。效果圖如下:

  登錄前:

  登陸后:

  實現代碼如下:

  1 package com.xingoo.plugin.swttest.test;
  2 
  3 import org.eclipse.swt.SWT;
  4 import org.eclipse.swt.events.SelectionAdapter;
  5 import org.eclipse.swt.events.SelectionEvent;
  6 import org.eclipse.swt.layout.FillLayout;
  7 import org.eclipse.swt.layout.GridData;
  8 import org.eclipse.swt.layout.GridLayout;
  9 import org.eclipse.swt.widgets.Button;
 10 import org.eclipse.swt.widgets.Combo;
 11 import org.eclipse.swt.widgets.Composite;
 12 import org.eclipse.swt.widgets.Group;
 13 import org.eclipse.swt.widgets.Label;
 14 import org.eclipse.swt.widgets.MessageBox;
 15 import org.eclipse.swt.widgets.Shell;
 16 import org.eclipse.swt.widgets.Text;
 17 
 18 import com.xingoo.plugin.swttest.Abstract.AbstractExample;
 19 
 20 public class Test extends AbstractExample{
 21     private Label infoLabel;
 22     private Text usernameText;
 23     private Text passwordText;
 24     private Combo roleCombo;
 25     
 26     public static void main(String[] args) {
 27         new Test().run();
 28     }
 29     public void todo(Shell shell) {
 30         Group testGroup = new Group(shell,SWT.NONE);
 31         testGroup.setText("User Login");
 32         GridLayout layout = new GridLayout();
 33         layout.numColumns = 2;
 34         layout.marginWidth = 30;
 35         layout.marginHeight = 10;
 36         testGroup.setLayout(layout);
 37         testGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 
 38         {
 39             Composite composite = new Composite(testGroup,SWT.NONE);
 40             GridLayout layoutComposite = new GridLayout();
 41             layoutComposite.numColumns = 2;
 42             layoutComposite.marginHeight = 1;
 43             composite.setLayout(layoutComposite);
 44             composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,2,2));
 45             
 46             infoLabel = new Label(composite,SWT.NONE);
 47             infoLabel.setText("請輸入用戶名 密碼");
 48             infoLabel.setLayoutData(new GridData(GridData.FILL_BOTH));
 49             infoLabel.setAlignment(SWT.RIGHT);
 50         }
 51         {
 52             Label usernameLabel = new Label(testGroup,SWT.NONE);
 53             usernameLabel.setText("username:");
 54             
 55             usernameText = new Text(testGroup,SWT.BORDER);
 56             usernameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
 57         }
 58         {
 59             Label passwordLabel = new Label(testGroup,SWT.NONE);
 60             passwordLabel.setText("password:");
 61             
 62             passwordText = new Text(testGroup,SWT.BORDER | SWT.PASSWORD);
 63             passwordText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
 64         }
 65         {
 66             Label roleLabel = new Label(testGroup,SWT.NONE);
 67             roleLabel.setText("role:");
 68             
 69             roleCombo = new Combo(testGroup,SWT.DROP_DOWN);
 70             roleCombo.setItems(new String[]{"Admin","custom"});
 71             roleCombo.select(1);
 72         }
 73         {
 74             new Label(testGroup,SWT.NONE);
 75             
 76             Button rememberPWBtn = new Button(testGroup,SWT.CHECK);
 77             rememberPWBtn.setText("記住密碼");
 78         }
 79         {
 80             new Label(testGroup,SWT.NONE);
 81             
 82             Button autoLoginBtn = new Button(testGroup,SWT.CHECK);
 83             autoLoginBtn.setText("自動登錄");
 84         }
 85         {
 86             new Label(testGroup,SWT.NONE);
 87             
 88             Button loginBtn = new Button(testGroup,SWT.PUSH);
 89             loginBtn.setText("登錄");
 90             
 91             loginBtn.addSelectionListener(new SelectionAdapter() {
 92                 public void widgetSelected(SelectionEvent evt){
 93                     infoLabel.setText("登陸成功");
 94                     
 95                     usernameText.setText("");
 96                     usernameText.setEnabled(false);
 97                     
 98                     passwordText.setText("");
 99                     passwordText.setEnabled(false);
100                     
101                     roleCombo.setEnabled(false);
102                 }
103             });
104         }
105     }
106 }

  注意其中的一些技巧:

  30-36行:我們創建了一個組控件,並且使用了網格布局,設置每行有兩列。並且設置了組內填充邊界,marginWidth以及marginHeight。

  39-49行:我們創建了一個組合對象,使他占有了兩個列元素。並且設置組內為兩列的網格布局。

  

  關於事件的監聽,之后也會搜集整理出一些常用的事件。

  剩下的就比較好理解了,當沒有空間元素填補的時候,為了防止布局錯亂,創建了一個空的Label對象用來占位。

  new Label(testGroup,SWT.NONE);

  

  這里面使用到了一個前文提到的抽象類,這里再貼出來一次。

 1 package com.xingoo.plugin.swttest.Abstract;
 2 
 3 import org.eclipse.swt.layout.FillLayout;
 4 import org.eclipse.swt.widgets.Display;
 5 import org.eclipse.swt.widgets.Shell;
 6 
 7 public abstract class AbstractExample{
 8     public void run(){
 9         Display display = new Display();
10         Shell shell = new Shell(display);
11         shell.setText("shell example");
12         shell.setBounds(200,200,400,280);
13         shell.setLayout(new FillLayout());
14         todo(shell);
15         shell.open();
16         
17         while(!shell.isDisposed()){
18             if(!display.readAndDispatch())
19                 display.sleep();
20         }
21         //dispose the resource
22         display.beep();
23         display.dispose();
24     }
25     public abstract void todo(Shell shell);//extension something here
26 }

  后續將會更新,復雜控件以及布局模型的介紹。


免責聲明!

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



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