2012-10-17 42 views
1

我想知道什麼是SWT複合對象的常規/常用實踐。SWT複合對齊

我發現,每當我添加了一個複合(與任何用戶界面例如:文本框或按鈕)在複合材料中產生的UI不對齊到複合的非常起始邊緣。 (您可以通過設置複合的背景色遵守本)

有複合材料中的文本框UI之前一些空間/填充。如果以前的UI未在Composite中創建,這會導致我創建的GUI窗體出現不對齊。

我想知道什麼是常見的做法,使它們一致?通過設置一些負填充來移動Composite,使其內的UI看起來像對齊?

下面的示例代碼!

public static void main(String[] args) { 
     Display display = new Display(); 
     Shell shell = new Shell(display); 
     GridLayout layout = new GridLayout(); 
     layout.numColumns = 1; 
     layout.makeColumnsEqualWidth = false; 
     shell.setLayout(layout); 

     Text t1 = new Text(shell, SWT.SINGLE | SWT.BORDER); 
     t1.setText("Test box..."); 

     Composite c = new Composite(shell, SWT.NONE); 
     // c.setBackground(new Color(shell.getDisplay(), 255,0,0)); 
     layout = new GridLayout(); 
     layout.numColumns = 2; 
     layout.makeColumnsEqualWidth = true; 
     c.setLayout(layout); 

     Text t2 = new Text(c, SWT.SINGLE | SWT.BORDER); 
     t2.setText("Test box within Composite... not aligned to the first textbox"); 

     Button b = new Button(c, SWT.PUSH); 
     b.setText("Button 1"); 

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

回答

6

這將解決這個問題:

public static void main(String[] args) { 
    Display display = new Display(); 
    Shell shell = new Shell(display); 
    shell.setLayout(new GridLayout(1, false)); 

    Text t1 = new Text(shell, SWT.SINGLE | SWT.BORDER); 
    t1.setText("Test box..."); 

    Composite c = new Composite(shell, SWT.NONE); 
    GridLayout layout = new GridLayout(2, true); 

    layout.marginWidth = 0; // <-- HERE 

    c.setLayout(layout); 

    Text t2 = new Text(c, SWT.SINGLE | SWT.BORDER); 
    t2.setText("Test box within Composite... not aligned to the first textbox"); 

    Button b = new Button(c, SWT.PUSH); 
    b.setText("Button 1"); 

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

只需設置marginWidth如果GridLayout0

enter image description here

只是一個提示:您可以設置列數和在GridLayout構造函數的等寬的事。