繁体   English   中英

如何在一个合成中使用两个网格布局

[英]How to use two Grid Layouts in one Composite

我在Composite遇到了一些麻烦。 我写了以下代码:

final Composite mavniOptionsComposite = new Composite(parent, SWT.NONE);
mavniOptionsComposite.setLayout(new GridLayout(3, false));

final GridData textGridData = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);

final Label mavniFormatingLabel = new Label(mavniOptionsComposite, SWT.None);
mavniFormatingLabel.setText(PFT.MAVNI_HISTORY.getExplanationLabelText());
mavniFormatingLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));

final GridData mavniFormatingGridData = new GridData(SWT.BEGINNING, SWT.CENTER, false, false);
final Button mavniHistoryRunCheckbox = new Button(mavniOptionsComposite, SWT.CHECK);
mavniHistoryRunCheckbox.setLayoutData(mavniFormatingGridData);
mavniHistoryRunCheckbox.setText(PFT.MAVNI_HISTORY.getExplanationLabelText());
mavniHistoryRunCheckbox.addSelectionListener(new mavniHistoryCheckBoxSelectionAdapter());
widgetsMap.put(PFT.MAVNI_HISTORY, mavniHistoryRunCheckbox);

final Button mavniRealtimeCheckbox = new Button(mavniOptionsComposite, SWT.CHECK);
mavniRealtimeCheckbox.setLayoutData(mavniFormatingGridData);
mavniRealtimeCheckbox.setText(PFT.MAVNI_REALTIME.getExplanationLabelText());
mavniRealtimeCheckbox.setEnabled(MyPreferences.isWmcDefined());
widgetsMap.put(PFT.MAVNI_REALTIME, mavniRealtimeCheckbox);

final Label toolNameLabel = new Label(mavniOptionsComposite, SWT.NONE);
toolNameLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
toolNameLabel.setText(PFT.TOOL_NAME.getExplanationLabelText());

final Text toolNameText = new Text(mavniOptionsComposite, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
toolNameText.setLayoutData(textGridData);
widgetsMap.put(PFT.TOOL_NAME, toolNameText);

final Label toolVersionLabel = new Label(mavniOptionsComposite, SWT.NONE);
toolVersionLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
toolVersionLabel.setText(PFT.TOOL_VERSION.getExplanationLabelText());

final Text toolVersionText = new Text(mavniOptionsComposite, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
toolVersionText.setLayoutData(textGridData);
widgetsMap.put(PFT.TOOL_VERSION, toolVersionText);

mavniOptionsComposite.setLayout(new GridLayout(2, false));

new MavniOptionViewer(MyPreferences.getMavniOptionsList()).createViewer(mavniOptionsComposite);

createExpandItem(parent, mavniOptionsComposite, "Mavni Options", com.mavni.ui.Activator
        .getDefault().getImageRegistry().get(IImageKeys.EXECUTE.getPath()), false);

但是我得到以下行为:

我希望表格扩展到完整布局。 据我了解,这是因为我设置了setLayout(new GridLayout(3, false)); 该表仅包含两列。 如果我设置mavniOptionsComposite.setLayout(new GridLayout(2, false)); 那么表格可以按预期工作,但其他元素在该位置未显示任何内容(就像现在应该显示的那样)。

通缉的行为:

起初,我虽然要创建两个GridLayouts并将它们插入一个复合中。 我试图寻找一种将两种布局组合在一个复合物中的方法,但是找不到一种方法。 我需要一个组合,因为createExpandItem仅可用于一个组合。 怎么解决呢?

编辑 :在MavniOptionViewer我有:

public void createViewer(final Composite parent) {
    commandsTable = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION);
    commandsTable.setHeaderVisible(true);
    commandsTable.setLinesVisible(true);
    final GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 5);
    data.heightHint = 120;
    commandsTable.setLayoutData(data);
    final Map<ButtonTypeEnum, Button> buttons = ButtonComboFactory.createAddRemoveCombo(parent);
    final Button add = buttons.get(ButtonTypeEnum.ADD);
    final Button remove = buttons.get(ButtonTypeEnum.REMOVE);
    ...

我虽然有另一种可能的解决方案。 是否可以更改createViewer以便该表具有3列,并且add remove按钮将位于分隔线上。 怎么做?

复合结构只能有一个布局,您不能半途更改它。 它将最终使用您为所有内容设置的最后一个布局。

您可以在一个包含的Composite中使用多个Composite。

您可以将主Composite保留为3列的GridLayout ,并告诉表格占据所有这3列,而不是像现在这样占用2列。

您可以通过将表格GridDatahorizontal span从2更改为3来实现:

final GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 3, 5);

另外,您不应该重用GridData对象。 文档中

GridLayout管理的Composite中的每个控件都必须具有唯一的GridData对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM