org.eclipse.swt.widgets.Composite setSize无法正常工作

问题描述 投票:0回答:1

我目前正在开发一个自定义插件,即使存在其他Composite元素,我也不明白为什么Composite元素会占用所有空间。

我的插件执行处理程序如下:

public class IwokGeneratorHandler extends AbstractHandler {

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {

        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
        Shell shell = window.getShell();
        ApplicationWindow win = new ApplicationWindow(shell) {

            @Override
            protected Control createContents(Composite parent) {

                parent.setSize(800, 600);
                //first panel
                Composite composite = new Composite(parent, SWT.NONE);
                //x, y, width, height
                composite.setBounds(10, 10, 424, 70);

                //second panel
                Composite composite_1 = new Composite(parent, SWT.NONE);
                composite_1.setBounds(10, 86, 424, 309);

                //third panel
                Composite composite_2 = new Composite(parent, SWT.NONE);
                composite_2.setBounds(10, 407, 424, 42);

                return parent;
            }
        };
        win.open();
        return null;
    }
}

但是,第一个Composite占用了Application主窗口的所有空间,而其他窗口大小无论如何都看不到。我检查了多次。

我是否缺少任何属性来阻止元素填充?

提前谢谢您

java eclipse eclipse-plugin swt jface
1个回答
2
投票

[ApplicationWindow期望createContents方法返回包含内容中所有控件的单个Composite(与大多数其他JFace窗口和对话框类一样)。

所以类似:

  protected Control createContents(final Composite parent) {

      parent.setSize(800, 600);

      // Main body composite
      Composite body = new Composite(parent, SWT.NONE);

      //first panel
      Composite composite = new Composite(body, SWT.NONE);
      //x, y, width, height
      composite.setBounds(10, 10, 424, 70);

      //second panel
      Composite composite_1 = new Composite(body, SWT.NONE);
      composite_1.setBounds(10, 86, 424, 309);

      //third panel
      Composite composite_2 = new Composite(body, SWT.NONE);
      composite_2.setBounds(10, 407, 424, 42);

      // Return the body
      return body;
  }

请注意,您的代码也返回了parent,这是错误的-它必须是创建的组合。

注意:尽管开始使用setBounds看起来很简单,但是如果代码在具有不同字体大小的不同机器上运行(如果在macOS / Linux / Windows上运行则是控件大小),它将引起问题。强烈建议使用布局。

© www.soinside.com 2019 - 2024. All rights reserved.