我们如何在 SWT 中自动调整组件的大小? [英] How can we auto resize the size of components in SWT?

查看:75
本文介绍了我们如何在 SWT 中自动调整组件的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 SWT 应用程序中,我在 SWT 外壳中有某些组件.

In my SWT application i have certain components inside the SWT shell.

现在我如何根据显示窗口的大小自动重新调整这些组件的大小.

Now how can i auto re-size this components according to the size of display window.

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);

    ...
}

即当我减小窗口的大小时,它里面的组件应该会相应地出现.

i.e when I decrease the size of window the components inside it should appear according to that.

推荐答案

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

That sounds suspiciously like you aren't using layouts.

布局的整个概念使得不必担心调整大小.布局将处理其所有组件的大小.

The whole concept of layouts makes worrying about resizing needless. The layout will take care of the size of all of its components.

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

您的代码可以轻松更正.不要设置单个组件的大小,布局将决定它们的大小.如果您希望窗口具有预定义的大小,请设置外壳的大小:

Your code can easily be corrected. Don't set the size of individual components, the layout will determine their size. If you want the window to have a predefined size, set the shell's size:

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();
}

调整大小之前:

调整大小后:

这篇关于我们如何在 SWT 中自动调整组件的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆