简体   繁体   English

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

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

In my SWT application i have certain components inside the SWT shell. 在我的SWT应用程序中,我在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);

    ...
}

ie 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. 布局将考虑其所有组件的大小。

I would recommend to read the Eclipse article about layouts 我建议阅读关于布局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: 如果您希望窗口具有预定义的大小,请设置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();
}

Before resizing: 在调整大小之前:

在调整大小之前

After resizing: 调整大小后:

调整大小后

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

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