2016-08-31 34 views
0

我在Eclipse RCP应用程序中创建了一个简单的SWT容器,并且在显示它的位置/顺序时遇到问题。这是我正在使用的代码。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应用程序中,我没有对我的父级组合进行任何其他处理。

回答

0

你不说什么createControls是一部分。这是一个MPart

我不能真正重现这个问题,但是你的代码中有很多问题可能导致它。

从不更改传递到您的代码(parent这里)的Composite的布局。创建另一个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支持。

+0

感谢您的指点。我不打算使用Color,我只是用它看到容器,为什么他们不在正确的位置。 (尽管我不知道如何处理它们,谢谢)是的,createControls是一个MPart。我试过你的代码,我的容器仍然从底部出现。仍然不知道为什么会发生这种情况 –

+0

所以我想出了问题。我没有发布代码,因为我正在查看错误的类,并且我认为它没有做任何事情,但是我将父项传递给另一个将画布添加到其中的方法。对不起菜鸟的错误,但感谢有用的指针。 –