编程语言
首页 > 编程语言> > java-ExpandBar无法与GridLayout一起使用

java-ExpandBar无法与GridLayout一起使用

作者:互联网

我正在用Java和SWT创建应用程序,但是我遇到了问题.我想将ExpandBar放入GridLayout.当我不折叠扩展栏时,一切都很好.当我这样做时,它可以正常折叠,但是不会重新布局复合材料.以下屏幕应解释我的问题:

崩溃前:

崩溃后:

我尝试了一些技巧,例如:

bar.addExpandListener(new ExpandListener() {
    public void itemExpanded(ExpandEvent e) {
        item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
        gridData2.heightHint = composite.computeSize(SWT.DEFAULT,
                SWT.DEFAULT).y;
        comp.layout(true, true);
    }

    public void itemCollapsed(ExpandEvent e) {
        item0.setHeight(item0.getHeaderHeight());
        gridData2.heightHint = item0.getHeaderHeight();
        comp.layout(true, true);
    }
});

完全不起作用.这是SSCCE:

package stackoverflow;

import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
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.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;

public class MainWindowSWT extends Window {

    public MainWindowSWT() {
        super((Shell)null);

        setBlockOnOpen(true);
        open();
        Display.getCurrent().dispose();
    }

    @Override
    protected Control createContents(Composite parent) {
        final Composite comp = new Composite(getShell(), 0);

        GridLayout layout = new GridLayout(1, true);
        comp.setLayout(layout);

        Tree tree = new Tree(comp, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
        GridData gridData = new GridData();
        gridData.verticalAlignment = GridData.FILL;
        gridData.verticalSpan = 2;
        gridData.grabExcessVerticalSpace = true;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        tree.setLayoutData(gridData);

        final ExpandBar bar = new ExpandBar(comp, SWT.V_SCROLL);
        final Composite composite = new Composite(bar, SWT.NONE);
        GridLayout gridLayout = new GridLayout();
        gridLayout.verticalSpacing = 10;
        composite.setLayout(gridLayout);
        Button button = new Button(composite, SWT.PUSH);
        button.setText("SWT.PUSH");
        final ExpandItem item0 = new ExpandItem(bar, SWT.NONE, 0);
        item0.setText("Test");
        item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
        item0.setControl(composite);

        item0.setExpanded(true);

        final GridData gridData2 = new GridData();
        gridData2.horizontalAlignment = GridData.FILL;
        gridData2.grabExcessHorizontalSpace = true;
        bar.setLayoutData(gridData2);

        return parent;
    }
}

Ubuntu 12.04.1 amd64,SWT 3.7.2.

提前致谢.

//顺便说一句:我不想调整shell的大小.我想调整扩展栏上方的树的大小.

解决方法:

好的,所以我找到了一个有点棘手的解决方案,但是它可行:

public class MainWindowSWT {

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        shell.setText("ExpandBar Example");
        final ExpandBar bar = new ExpandBar(shell, SWT.NONE);

        final Composite composite = new Composite(bar, SWT.NONE);
        GridLayout layout = new GridLayout();
        composite.setLayout(layout);
        Button button = new Button(composite, SWT.PUSH);
        button.setText("SWT.PUSH");
        ExpandItem item0 = new ExpandItem(bar, SWT.NONE, 0);
        item0.setText("What is your favorite button");
        item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
        item0.setControl(composite);

        item0.setExpanded(true);

        bar.setSpacing(8);

        bar.addExpandListener(new ExpandListener() {

            @Override
            public void itemExpanded(ExpandEvent e) {
                if (e.item instanceof ExpandItem) {
                    ExpandItem item = (ExpandItem) e.item;
                    shell.setSize(shell.getSize().x, shell.getSize().y + item.getHeight());
                }
            }

            @Override
            public void itemCollapsed(ExpandEvent e) {
                if (e.item instanceof ExpandItem) {
                    ExpandItem item = (ExpandItem) e.item;
                    shell.setSize(shell.getSize().x, shell.getSize().y - item.getHeight());
                }
            }
        });
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}

基本上,您可以强制外壳增大/减小其高度以反映更改.我希望有一种更简单/更漂亮的方法,但是目前我什么都没想到.

它部分基于this答案,但不使用线程.

标签:grid-layout,swt,ubuntu-12-04,layout-manager,java
来源: https://codeday.me/bug/20191127/2076073.html