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

发布于 2024-12-15 06:54:02 字数 1290 浏览 7 评论 0原文

我需要在 Eclipse rcp 中的 Scrollable Composite 上绘制多个画布,并将它们放入网格布局中。我设法实现了这一点,但我坚持调整 GridLayouts 单元格大小。

我想要的是网格动态调整每个画布的大小(或者至少手动设置大小),但使 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 对象,但我在这里有点迷失。

I need to draw multiple canvases on a Scrollable Composite in Eclipse rcp, and put them in a gridlayout. I managed to achieve this, but I'm stuck with adjusting the GridLayouts Cell-Size.

What I would like to have is for the grid to dynamically adjust the size of each canvas (or at least set the size manually) but make the GridLayout fill the complete space of the parent component.

What I have so far:

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();
      }
    }
  }
}

I assumed that setting the Parent Component's layout to FillLayout would do the trick, but it doesn't work. I think I need to work with a GridData Object, but I'm a little lost here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

噩梦成真你也成魔 2024-12-22 06:54:02

您可以创建两个 GridData 对象并为其设置值,一个用于非活动单元格,一个用于活动单元格。例如:

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);

you can create two GridData Objects and set values for them, one for Deactive Cells and one for Active Cells. for example:

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

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

then in the Listener Method you can manage the current Canvas Cell-Size by set its LayoutData, like:

c.setLayoutData(Active);

or

c.setLayoutData(Deactive);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文