【发布时间】:2021-02-25 11:27:06
【问题描述】:
我有一个包含两个嵌入式组的 SWT 组。一次只能看到其中一个,我们可以通过按钮在它们之间切换。 问题是,我希望它们在可见时都填满整个空间,但它们没有这样做。
package guitables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
public class GuiTables {
private static Group uIWrapperGroup;
private static Group uGroup;
private static Group iPGroup;
private static ExpandBar expandBar;
private static ExpandItem expandItem;
private static Button switchToUButton;
private static Button switchToIButton;
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
create(shell);
}
private static void create(final Shell shell) {
expandBar = new ExpandBar(shell, SWT.NONE);
expandItem = new ExpandItem(expandBar, SWT.NONE);
expandItem.setExpanded(true);
uIWrapperGroup = new Group(shell, SWT.VERTICAL);
uIWrapperGroup.setText("");
uIWrapperGroup.setLayout(new GridLayout(21, false));
uIWrapperGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
uGroup = new Group(uIWrapperGroup, SWT.NONE);
uGroup.setText("");
uGroup.setLayout(new GridLayout(21, false));
createUButtons(uGroup);
iPGroup = new Group(uIWrapperGroup, SWT.NONE);
iPGroup.setText("");
iPGroup.setLayout(new GridLayout(21, false));
iPGroup.setVisible(false);
createIPButtons(iPGroup);
}
private static void createUButtons(Composite parent) {
switchToIButton = new Button(parent, SWT.NONE);;
switchToIButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
switchToIP();
}
});
}
private static void createIPButtons(Composite parent) {
switchToUButton = new Button(parent, SWT.NONE);
switchToUButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
switchToU();
}
});
}
private static void switchToIP() {
uGroup.setVisible(false);
GridData invisible = new GridData(0,0);
invisible.exclude = true;
uGroup.setLayoutData(invisible);
uGroup.setVisible(false);
GridData visible = new GridData(SWT.FILL, SWT.FILL, true, true);
visible.exclude=false;
iPGroup.setLayoutData(visible);
iPGroup.setVisible(true);
reLayoutExpandItem(expandItem, uIWrapperGroup, SWT.DEFAULT);
}
private static void switchToU() {
iPGroup.setVisible(false);
GridData invisible = new GridData(0,0);
invisible.exclude = true;
iPGroup.setLayoutData(invisible);
iPGroup.setVisible(false);
GridData visible = new GridData(SWT.FILL, SWT.FILL, true, true);
visible.exclude=false;
uGroup.setLayoutData(visible);
uGroup.setVisible(true);
reLayoutExpandItem(expandItem, uIWrapperGroup, SWT.DEFAULT);
}
public static void reLayoutExpandItem(ExpandItem expandItem, Composite composite, int maxHeight) {
if (!composite.isDisposed()) {
int height = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
if(SWT.DEFAULT != maxHeight && height > maxHeight) {
expandItem.setHeight(maxHeight);
} else {
expandItem.setHeight(height);
}
}
}
}
一切正常,除了两个嵌入组不会水平填充可用空间,而是最终朝向左侧,并在右侧可用空间结束之前停止。当只有一个嵌入组时,它会按照我想要的方式填充整个空间。 我可以查看哪些内容?
【问题讨论】:
-
可能您需要使用
GridData.exclude,但由于您没有提供合适的minimal reproducible example 进行测试,我无法确定 -
我添加了更多代码以使其更具重现性。
-
如果您更改某项的
exclude值或更改 GridData,您必须在父 Composite 上调用layout(true)以使其生效 -
我添加了它,但仍然没有任何变化。
-
该代码无法编译。