2017-04-09 64 views
1

工作,這就是我想實現,使用SWT:行佈局不嵌套複合

The goal

對於這一點,我想使用RowLayout嵌套的Composite,包裝複合材料的根據可用空間進行控制。下面的代碼工作完美:

public class RowLayoutExample { 

    public static void main(String[] args) { 
     Display display = new Display(); 
     Shell shell = new Shell(display); 
     shell.setText("SWT RowLayout test"); 

     shell.setLayout(new RowLayout(SWT.HORIZONTAL)); 

     for (int i = 0; i < 10; i++) { 
      new Label(shell, SWT.NONE).setText("Label " + i); 
     } 

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

這顯示(請注意下一行的最後一個標籤的很好的總結 - 此外,在外殼調整,組件封裝到可用的水平空間):

Row layout on shell works!

當我這樣做,反而:

public class RowLayoutExample { 

    public static void main(String[] args) { 
     Display display = new Display(); 
     Shell shell = new Shell(display); 
     shell.setText("SWT RowLayout test"); 

     shell.setLayout(new RowLayout(SWT.HORIZONTAL)); 
     Composite comp = new Composite(shell, SWT.NONE); 
     comp.setLayout(new RowLayout(SWT.HORIZONTAL)); 

     for (int i = 0; i < 10; i++) { 
      new Label(comp, SWT.NONE).setText("Label " + i); 
     } 

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

我有以下行爲。如果我調整外殼的大小,標籤不會包裝成多行。

Row layout no longer works on shell control

在下面的圖片中,我們可以看到,複合材料膨脹出殼客戶端的面積,而不是換行到第二行。調整外殼大小不會影響這種錯誤行爲。

enter image description here

我使用下面的SWT版本:

<dependency> 
    <groupId>org.eclipse.swt</groupId> 
    <artifactId>org.eclipse.swt.cocoa.macosx.x86_64</artifactId> 
    <version>4.3</version> 
</dependency> 

那麼,爲什麼第二種情況是不工作?此外,是否可以使用殼牌GridLayout,但是RowLayout是否適用於此殼牌的小孩?

+0

無論你喜歡,你都可以混合佈局。 –

+0

好的,這也是我的猜測,但爲什麼嵌套的複合RowLayout不工作,而外殼的RowLayout是? – hypercube

+0

可能與Shell RowLayout如何請求子複合來計算其大小有關。 –

回答

3

下面是一個使用GridLayoutShell的佈局爲例:

public static void main(String[] args) { 
    Display display = new Display(); 
    Shell shell = new Shell(display); 
    shell.setText("SWT RowLayout test"); 

    shell.setLayout(new GridLayout()); 
    Composite comp = new Composite(shell, SWT.NONE); 
    comp.setLayout(new RowLayout(SWT.HORIZONTAL)); 
    comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 

    for (int i = 0; i < 10; i++) { 
     new Label(comp, SWT.NONE).setText("Label " + i); 
    } 

    shell.setSize(400, 250); 
    shell.open(); 

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

產生相同的結果作爲你的第一個例子。

「竅門」是將GridData設置爲GridLayout元素的子元素。

+1

真棒迴應! LayoutData是必要的,但在我的情況下是不夠的。我還在與設置RollLayout()的同一個組合上搜索了文本,並且此搜索文本爲其設置了GridData。這是主要問題,並在RollLayout中給我一個錯誤,因爲它的構造函數期望此容器的所有子項都有一個RowData。因爲這個,我一整天都在黑暗中思索。非常感謝你! – hypercube