调整GridLayout的单元格大小 - 绘制多个画布?

时间:2011-11-14 12:59:21

标签: eclipse layout canvas rcp grid-layout

我需要在Eclipse rcp中的Scrollable Composite上绘制多个画布,并将它们放在gridlayout中。我设法实现了这一点,但我仍然坚持调整GridLayouts Cell-Size。

我希望网格能够动态调整每个画布的大小(或至少手动设置大小),但要使GridLayout填充父组件的完整空间。

到目前为止我所拥有的:

public class Test {
  public static void main(final String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);

    shell.setLayout(new FillLayout());

    final ScrolledComposite sComp = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL);
    final Composite fillComp = new Composite(sComp, SWT.NONE);

    GridLayoutFactory.createFrom(new GridLayout(10, true)).applyTo(fillComp);

    for (int i = 0; i < 500; i++) {
      final Canvas c = new Canvas(fillComp, SWT.DOUBLE_BUFFERED);
      c.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent e) {
          e.gc.setBackground(display.getSystemColor(new Random().nextInt(20)));
          e.gc.fillRectangle(0, 0, 200, 200);
        }
      });
    }

    sComp.setContent(fillComp);

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

我认为将父组件的布局设置为FillLayout会起到作用,但它不起作用。我想我需要使用GridData对象,但我在这里有点迷失。

1 个答案:

答案 0 :(得分:0)

您可以创建两个GridData对象并为它们设置值,一个用于Deactive Cells,另一个用于Active Cells。例如:

GridData Acrive = new GridData();
Active.widthHint = 200;
Active.heightHint = 200;

GridData Deactive = new GridData();
Deactive.widthHint = 100;
Deactive.heightHint = 100;

然后在监听器方法中,您可以通过设置其LayoutData来管理当前的Canvas Cell-Size,如:

c.setLayoutData(Active);

c.setLayoutData(Deactive);