繁体   English   中英

我们如何自动调整SWT中组件的大小?

[英]How can we auto resize the size of components in SWT?

在我的SWT应用程序中,我在SWT shell中有一些组件。

现在,我如何根据显示窗口的大小自动重新调整此组件的大小。

Display display = new Display();

Shell shell = new Shell(display);
Group outerGroup,lowerGroup;
Text text;

public test1() {
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns=1;
    shell.setLayout(gridLayout);

    outerGroup = new Group(shell, SWT.NONE);

    GridData data = new GridData(1000,400);
    data.verticalSpan = 2;
    outerGroup.setLayoutData(data);    

    gridLayout = new GridLayout();

    gridLayout.numColumns=2;
    gridLayout.makeColumnsEqualWidth=true;
    outerGroup.setLayout(gridLayout);

    ...
}

即,当我减小窗口的大小时,它内部的组件应该根据它出现。

这听起来很可疑,就像你没有使用布局一样。

布局的整个概念令人担心无法重新调整大小。 布局将考虑其所有组件的大小。

我建议阅读关于布局Eclipse文章

您的代码很容易纠正。 不要设置单个组件的大小,布局将决定它们的大小。 如果您希望窗口具有预定义的大小,请设置shell的大小:

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));

    Group outerGroup = new Group(shell, SWT.NONE);

    // Tell the group to stretch in all directions
    outerGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    outerGroup.setLayout(new GridLayout(2, true));
    outerGroup.setText("Group");

    Button left = new Button(outerGroup, SWT.PUSH);
    left.setText("Left");

    // Tell the button to stretch in all directions
    left.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Button right = new Button(outerGroup, SWT.PUSH);
    right.setText("Right");

    // Tell the button to stretch in all directions
    right.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    shell.setSize(1000,400);
    shell.open();

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

在调整大小之前:

在调整大小之前

调整大小后:

调整大小后

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM