显示RCP的SWT复合问题

时间:2016-08-31 21:07:59

标签: swt eclipse-rcp rcp

我正在Eclipse RCP应用程序中创建一个简单的SWT容器,并且在显示它的位置/顺序方面存在问题。这是我正在使用的代码。

@PostConstruct
public void createControls(Composite parent)
{
     parent.setBackground(new Color (Display.getCurrent (), 255, 144, 0));
     GridData parentData = new GridData(SWT.FILL, SWT.FILL, true, true);
     parent.setLayout(new GridLayout(1, true));
     parent.setLayoutData(parentData);
     Device device = Display.getCurrent ();
     Color backgroundColor = parent.getBackground();
     Color whiteColor = new Color (device, 255, 255, 255);
     Color randomColor = new Color (device, 255, 0, 0);

     final Composite criteriaContainer = new Composite(parent, SWT.BORDER);
     criteriaContainer.setBackground(randomColor);
     final GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
     final GridData comboGridData = new GridData(SWT.FILL, SWT.TOP, true, false,1,1);

     criteriaContainer.setLayout(new GridLayout(1, false));
     criteriaContainer.setLayoutData(gridData);

     final Label primaryComboLabel = new Label(criteriaContainer, SWT.NONE);
     primaryComboLabel.setLayoutData(comboGridData);
     primaryComboLabel.setForeground(whiteColor);
     primaryComboLabel.setText("View by:");
     criteriaContainer.layout();
     criteriaContainer.pack();
     parent.layout();
     parent.pack();
}

我似乎无法将标签显示在应用程序的顶部(显示在底部中心)[在此处输入图像描述] [1]。如果我编写相同的代码并将其作为独立的SWT应用程序执行,则标签显示在左上角。 [1]:http://i.stack.imgur.com/OL312.png 这是我有一个独立的swt应用程序的区别。

public void showHistoryContainer() throws Exception {
        Display display = new Display();
        final Shell shell = new Shell(display); 
        shell.setBackground(new Color (Display.getCurrent (), 255, 144, 0));

其余代码是相同的。 [2]:http://i.stack.imgur.com/fJmR6.png

为什么会发生这种情况的任何想法?

注意:在我的RCP应用程序中,我没有对我的父组合进行任何其他处理。

1 个答案:

答案 0 :(得分:0)

你不能说createControls是其中的一部分。它是MPart吗?

我无法真正重现此问题,但您的代码中存在许多可能导致此问题的问题。

从不更改传递给您代码的Composite的布局(此处为parent)。作为孩子创建另一个Composite并在其上设置布局。

请勿拨打layoutpack

简化我的代码:

@PostConstruct
public void postConstruct(Composite parent)
{
  Display device = parent.getDisplay();

  Composite body = new Composite(parent, SWT.NONE);
  body.setBackground(new Color(device, 255, 144, 0));
  body.setLayout(new GridLayout());

  Color whiteColor = new Color(device, 255, 255, 255);
  Color randomColor = new Color(device, 255, 0, 0);

  Composite criteriaContainer = new Composite(body, SWT.BORDER);
  criteriaContainer.setBackground(randomColor);
  criteriaContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
  criteriaContainer.setLayout(new GridLayout());

  Label primaryComboLabel = new Label(criteriaContainer, SWT.NONE);
  primaryComboLabel.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
  primaryComboLabel.setForeground(whiteColor);
  primaryComboLabel.setText("View by:");
}

最后,如果你创建这样的Color个对象,你必须安排处理它们,否则你会泄漏资源。如果这是e4 RCP,您应该使用CSS支持。

相关问题