2011-12-28 120 views
0

我正在將我們的應用程序中的許多SWT標籤轉換爲文本小部件,因爲我們需要使我們的軟件更易於訪問。如果我們使用只讀文本小部件,屏幕閱讀器很難閱讀標籤值,但可以輕鬆進行。SWT佈局 - 無法在GridLayout中垂直居中Text小部件

將網格佈局中的多個標籤轉換爲只讀文本後,我們似乎無法使文本垂直居中。下面是一個簡單的計算器例子,我們看到了相同的行爲。我已經在GridData和Text上玩過SWT.CENTER,SWT.FILL,但似乎無法使控件垂直居中。

public void initUI() { 

     GridLayout g1 = new GridLayout(4, true); 
     g1.horizontalSpacing = 4; 
     g1.verticalSpacing = 4; 
     g1.marginBottom = 5; 
     g1.marginTop = 5; 
     shell.setLayout(g1); 

     String[] buttons = { 
       "Cls", "Bck", "", "Close", "7", "8", "9", "/", "4", 
       "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" 
     }; 

     Text display = new Text(shell, SWT.SINGLE); 
     GridData gridData = new GridData(); 
     gridData.horizontalSpan = 4; 
     gridData.horizontalAlignment = GridData.FILL; 
     display.setLayoutData(gridData); 

     for (int i = 0; i < buttons.length; i++) { 

      if (i == 2) { 
       Label lbl = new Label(shell, SWT.CENTER); 
       GridData gd = new GridData(SWT.FILL, SWT.FILL, false, false); 
       lbl.setLayoutData(gd); 
      } else { 
       Text btn = new Text(shell,SWT.SINGLE | SWT.READ_ONLY | SWT.BORDER); 
       btn.setText(buttons[i]); 
       GridData gd = new GridData(SWT.CENTER, SWT.CENTER, true, true); 

       gd.widthHint = 50; 
       gd.heightHint = 30; 
       btn.setLayoutData(gd); 
       btn.selectAll(); 


      } 
     } 

    } 

回答

2

我想你的意思是你想要的文字水平中心(它已經垂直居中)。進行此項更改,它將爲軸心的方式(我加了SWT.CENTERText):

Text btn = new Text(shell, SWT.SINGLE | SWT.READ_ONLY 
    | SWT.BORDER | SWT.CENTER); 
0

下面是對我的作品的簡單方法。

Label label = new Label(composite, SWT.CENTER); 
label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 
label.setText("\nText"); 

黑客是後綴新行(\ n)的文字。