如何使窗口能够在SWT中变薄?

时间:2014-02-20 09:02:32

标签: java swt

为什么以下应用程序不允许我使窗口非常薄?最小宽度允许布置3列图像,同时我希望能够实现单列宽。

enter image description here

如何缩小范围?

package tests;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class TryRowLayout {

    public static void main(String[] args) {

        RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
        rowLayout.wrap = true;





        Display display = new Display();

        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        shell.setMinimumSize(1, 1);
        //shell.setLayout(rowLayout);

        Composite composite = new Composite(shell, SWT.NONE);
        composite.setLayout(rowLayout);




        Image image = new Image(display, "images/alt_window_32.gif");

        Label label;
        for(int i=0; i<100; ++i) {
            //label = new Label(shell, SWT.NONE);
            label = new Label(composite, SWT.NONE);
            label.setImage(image);
        }



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

    }


}

1 个答案:

答案 0 :(得分:5)

原因是Windows需要最小窗口宽度才能添加最小/最大/关闭按钮和窗口标题。

Shell的默认样式是

SWT.SHELL_TRIM = SWT.CLOSE | SWT.TITLE | SWT.MIN | SWT.MAX | SWT.RESIZE

不幸的是,你甚至无法通过强迫Shell只显示关闭按钮来解决这个问题:

Shell shell = new Shell(display, SWT.CLOSE | SWT.RESIZE);

Windows仍会强制执行最小宽度。


总结,如果你仍然需要窗口控件,我担心你无能为力。如果您不需要窗口控件,则可以使用

Shell shell = new Shell(display, SWT.RESIZE);

以下是示例代码:

public static void main(String[] args)
{
    RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
    rowLayout.wrap = true;

    Display display = new Display();

    Shell shell = new Shell(display, SWT.RESIZE);
    shell.setLayout(new FillLayout());
    shell.setMinimumSize(1, 1);
    // shell.setLayout(rowLayout);

    Composite composite = new Composite(shell, SWT.NONE);
    composite.setLayout(rowLayout);

    Label label;
    for (int i = 0; i < 100; ++i)
    {
        // label = new Label(shell, SWT.NONE);
        label = new Label(composite, SWT.NONE);
        label.setText("A");
    }

    shell.pack();
    shell.open();
    shell.setSize(50, 200);
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
}

这就是它的样子:

enter image description here