簡體   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