SWT 为什么在我在父级中创建新的 Composite 后,父级 Composite 会调整大小?
SWT Why is parent Composite resized after I create a new Composite in parent?
我想重新创建 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, SWT.FILL, true, 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();
}
行数:
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
。
我想重新创建 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, SWT.FILL, true, 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();
}
行数:
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
。