SWT 为什么在父级中创建新的 Composite 后父级 Composite 会调整大小?

问题描述

我想重新创建 ExpandItem 的功能和外观。我正在创建一个 Composite,它有一个 StyledText 小部件和一个 Button。当按下 Button 时,会在这两个下方创建一个新的 Composite,再次按下时,这个新的 Composite 将被隐藏。但是,在创建新的 Composite 后,我的父 Composite 无缘无故地调整了大小。我附上了我的代码和我的工作图片。我将不胜感激。

Shell shell = new Shell();
//    shell.setSize(400,400);
Display display = shell.getDisplay();
shell.setLayout(new FillLayout());

//create outerComp
GridLayout gridLayoutOuterComp = new GridLayout();

GridData gridDataOuterComp = new GridData(SWT.FILL,SWT.FILL,true,true);
gridDataOuterComp.heightHint = 150;

Composite outerComp = new Composite(shell,SWT.NONE);
outerComp.setLayout(gridLayoutOuterComp);
outerComp.setLayoutData(gridDataOuterComp);

//create innerComp
GridLayout gridLayoutInnerComposite = new GridLayout();
gridLayoutInnerComposite.numColumns = 5;

GridData gridDataInnerComposite = new GridData(SWT.FILL,false);
gridDataInnerComposite.heightHint = 40;

Composite composite = new Composite(outerComp,SWT.NONE);
composite.setLayout(gridLayoutInnerComposite);
composite.setLayoutData(gridDataInnerComposite);
composite.setBackground(display.getSystemColor(SWT.COLOR_GREEN));

// create the styled text widget
GridData gridDataStyledText = new GridData();
gridDataStyledText.grabExcessHorizontalSpace = true;
gridDataStyledText.horizontalSpan = 4;
gridDataStyledText.horizontalAlignment = SWT.FILL;
gridDataStyledText.heightHint = 30;

StyledText widget = new StyledText(composite,SWT.BORDER);
String textNew = "This is StyledText in Composite";
widget.setText(textNew);
widget.setLayoutData(gridDataStyledText);

//create Button
GridData gridDataButton = new GridData();
gridDataStyledText.horizontalSpan = 1;
gridDataButton.heightHint = 30;
gridDataButton.grabExcessHorizontalSpace = false;
gridDataButton.horizontalAlignment = SWT.END;

Button button = new Button(composite,SWT.PUSH);
button.setText("B1");
button.setLayoutData(gridDataButton);

button.addSelectionListener(new SelectionListener() {
  @Override
  public void widgetSelected(SelectionEvent e) {
    GridData newCompGridData = new GridData();
    if (counter % 2 == 1) {
      if (newComp != null && !newComp.isVisible()) {
        newComp.setVisible(true);

        GridData newData = new GridData();
        newData.heightHint = 100;
        composite.setLayoutData(newData);

        counter++;
      } else {
        newComp = new Composite(composite,SWT.BORDER);
        newCompGridData.horizontalAlignment = SWT.FILL;
        newCompGridData.grabExcessHorizontalSpace = true;
        newCompGridData.horizontalSpan = 5;
        newCompGridData.verticalAlignment = SWT.FILL;
        newComp.setLayoutData(newCompGridData);

        GridData newData = new GridData();
        newData.heightHint = 100;
        composite.setLayoutData(newData);

        Color red = display.getSystemColor(SWT.COLOR_RED);
        newComp.setBackground(red);


        counter++;
      }
    } else {
      newComp.setVisible(false);

      GridData newData = new GridData();
      newData.heightHint = 40;
      composite.setLayoutData(newData);

      counter++;
    }
    composite.getParent().layout();
    composite.layout();
    newComp.layout();
  }

Before resize

After resize

解决方法

行:

GridData newData = new GridData();
newData.heightHint = 100;
composite.setLayoutData(newData);

正在将 composite 的布局数据更改为默认对齐和抓取值。它们应该是:

GridData newData = new GridData(SWT.FILL,SWT.FILL,true,false);
newData.heightHint = 100;
composite.setLayoutData(newData);

注意:线

outerComp.setLayoutData(gridDataOuterComp);

什么都不做,因为 outerComp 的父级正在使用 FillLayout

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...