java - 未展开时 SWT 展开栏未位于正确的位置

标签 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();

    }

    }

最佳答案

行:

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

正在添加一个您不使用但占用了对话框中心空间的组合。删除它,它对我来说工作正常。

如果您想使用某些内容来分隔“查找”按钮,只需使用带有默认布局数据的空标签即可:

new Label(shell, SWT.NONE);

关于java - 未展开时 SWT 展开栏未位于正确的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31700718/

相关文章:

java - 在创建没有 setter 的不可变类时,为什么我要声明一个字段为 Final

java - 在 Java 中读取 CDATA XML

java - 安卓项目: Conversion to Dalvik format failed with error 1

java - 用户界面 : Month selector

java - 是否可以更改 SWT 中文本小部件的提示文本的颜色?

java - 可以将 swt.graphics.image 转换为 awt.image 吗?

java - 错误 : Non-static method 'findViewById(int)' cannot be referenced from a static context

java - 为文本框创建气球工具提示?

java - 如何在 SWT 中添加图像作为缩略图

java - Mockito:Java - 是否可以更改 InjectMocks 类中使用的变量值?