未展开时,SWT展开栏不在正确的位置

时间:2015-07-29 12:42:10

标签: java swt

我有以下用户界面: UI

首次加载Shell时,它不会出现在搜索按钮下方,尽管它会在布局中同时显示两列。 Initial UI

当窗口改变大小时,它最终会出现在正确的位置。像这样: How ui should look

以下是最小工作示例的代码:

package com.mycompany.swttest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;

public class BrowserTest {
    final Display display;
    final Shell shell;

    private Text f1, f2, f3,f4;
    private Label l1, l2, l3,l4; 
        private final ExpandItem resultDropdown; 
    private final Composite resultComposite; 
        Browser resultsBrowser;

    public BrowserTest() {
        display = new Display();
        shell = new Shell(display);


        //Listener to remove highlighting when this window closes

        this.shell.setText("Search");

        GridLayout thisLayout = new GridLayout(2, false);
        this.shell.setLayout(thisLayout);


        //SET MINIMUM WINDOW SIZES
        Rectangle screenBounds = this.shell.getDisplay().getBounds();
        Point size = new Point(screenBounds.width/2,screenBounds.height/2);

        this.shell.setMinimumSize(size);

        java.util.List<Label> labelList;
        java.util.List<Text> fieldList;
        java.util.List<String> textList;

        int thisStyle = (SWT.SINGLE | SWT.BORDER);

        GridData gd = new GridData();
        gd.horizontalAlignment = SWT.FILL;
        gd.grabExcessHorizontalSpace = true;


        l1 = new Label(this.shell, SWT.NULL);
        f1 = new Text(this.shell, thisStyle);
        f1.setLayoutData(gd);

        l2 = new Label(this.shell, SWT.NULL);
        f2 = new Text(this.shell, thisStyle);
        f2.setLayoutData(gd);

        l3 = new Label(this.shell, SWT.NULL);
        f3 = new Text(this.shell, thisStyle);
        f3.setLayoutData(gd);

        gd = new GridData();
        gd.horizontalAlignment = SWT.FILL;
        gd.verticalAlignment = SWT.FILL;
        gd.grabExcessHorizontalSpace = true;
        gd.grabExcessVerticalSpace = true;

        l4 = new Label(this.shell, SWT.NULL);
        f4 = new Text(this.shell, SWT.MULTI | SWT.BORDER);
        gd.verticalAlignment = SWT.FILL;
        f4.setLayoutData(gd);

        Composite comp = new Composite(shell, SWT.NONE);
        comp.setLayoutData(gd);

        labelList = new ArrayList(Arrays.asList(new Label[]{l1, l2, l3, l4}));
        fieldList = new ArrayList(Arrays.asList(new Text[]{f1, f2, f3, f4}));
        textList = new ArrayList(Arrays.asList(new String[]{"F1: ", "F2: ", "F3: ", "F4: "}));

        //For every label/field, set text and layout
        for (int i = 0; i < labelList.size(); i++) {
            Label thisLabel = labelList.get(i);
            String thisText = textList.get(i);
            Text thisField = fieldList.get(i);

            thisLabel.setText(thisText);
        }


        Button findButton = new Button(this.shell, SWT.PUSH| SWT.CENTER);
        findButton.setText("Find");


        gd = new GridData();
        gd.horizontalAlignment = SWT.RIGHT;
        gd.verticalAlignment = SWT.TOP;
        gd.verticalSpan = 1;

        findButton.setLayoutData(gd);


        //New set of GridData for our results section
        gd = new GridData();
        gd.horizontalAlignment = SWT.FILL;
        gd.verticalAlignment = SWT.FILL;
        gd.grabExcessHorizontalSpace = true;
        gd.grabExcessVerticalSpace = true;
        //gd.verticalSpan = 3;
        gd.horizontalSpan = 2;

        ExpandBar expandBar = new ExpandBar(this.shell, SWT.V_SCROLL);
        expandBar.setLayoutData(gd);

        resultDropdown = new ExpandItem(expandBar, SWT.NO_FOCUS);
        resultDropdown.setHeight(300);
        resultDropdown.setText("Results");

        resultComposite = new Composite(expandBar, SWT.NO_FOCUS);
        GridLayout gl = new GridLayout();
        gl.numColumns = 1;
        resultComposite.setLayout(gl);
        resultComposite.setLayoutData(gd);

        //Create a browser on a temp Composite
        resultsBrowser = new Browser(resultComposite, SWT.NONE);
        resultsBrowser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));


        resultDropdown.setControl(resultComposite);
        resultDropdown.setExpanded(false);

         this.shell.pack();

    }

    public void open() {
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        display.dispose();
    }

    public static void main(String[] args) {

        BrowserTest testBrowser = new BrowserTest();
        testBrowser.open();

    }

    }

1 个答案:

答案 0 :(得分:1)

行:

    Composite comp = new Composite(shell, SWT.NONE);
    comp.setLayoutData(gd);

正在添加您不会使用的复合材料,但正在耗尽对话框中心的空间。删除它,它对我来说没问题。

如果你想使用某些东西来寻找&#39;发现&#39;按钮只使用带有默认布局数据的空标签:

new Label(shell, SWT.NONE);