2017-04-09 66 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