Java 如何在 SWT 中设置最大合成尺寸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18549070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:57:38  来源:igfitidea点击:

How to set max composite size in SWT

javaeclipseswt

提问by John Smith

Is there any way to set maximal size of composite? Only what i have found methods

有没有办法设置复合材料的最大尺寸?只有我发现的方法

setMinimumSize(Point point)
setSize(Point point)

which allow me to set minimal and prefered size.

这允许我设置最小和首选大小。

采纳答案by Baz

As far as I know, there is no Layout, that has a setting for maximal size, which is a good thing in my opinion.

据我所知,没有 Layout,它有一个最大尺寸的设置,在我看来这是一件好事。

Consider the following szenario: You set the maximal size of a Widget/Compositeto a value that you think "looks good". Depending on the screen resolution and text size of the end-user, the chosen maximal size might just look wrong. This is why the layouts usually adapt to the available space.

考虑以下SZENARIO:您可以设定的最大尺寸Widget/Composite到一个值,你认为“很好看”。根据最终用户的屏幕分辨率和文本大小,选择的最大大小可能看起来是错误的。这就是为什么布局通常会适应可用空间的原因。



Nevertheless, here is some code, that restricts the size:

不过,这里有一些代码,限制了大小:

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

    final Button left = new Button(shell, SWT.PUSH);
    left.setText("Restricted width");
    left.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    left.addListener(SWT.Resize, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            Point size = left.getSize();

            if(size.x > 200)
            {
                left.setSize(200, size.y);
            }
        }
    });

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

Before resizing:

调整大小前:

enter image description here

在此处输入图片说明

After resizing:

调整大小后:

enter image description here

在此处输入图片说明

回答by benez

i am curious about why you want to set a maximum size. can you give a concrete example? i had a situation where i wanted this option, but i came up with a solution, that was even better. just think of a widget in a layout that has a preferred size based on its contents, e.g. a text widget. now you want that widget to be as big as the available space allows it. but since that widget is claiming space based on its contents, it might claim more space than necessary. so what i needed was a maximum size, but if more space is available, that widget will still be able to take the space without claiming more.

我很好奇你为什么要设置最大尺寸。你能举一个具体的例子吗?我遇到过想要这个选项的情况,但我想出了一个解决方案,那就更好了。只需考虑布局中的小部件,该小部件根据其内容具有首选大小,例如文本小部件。现在您希望该小部件与可用空间一样大。但由于该小部件根据其内容要求空间,因此它可能要求更多的空间。所以我需要的是最大尺寸,但如果有更多空间可用,该小部件仍然可以占用空间而不会占用更多空间。

but lets have a look at an example:

但让我们看一个例子:

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

    final Text text = new Text(shell, SWT.WRAP | SWT.MULTI | SWT.V_SCROLL);
    text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    text.setText(getLongText());

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

the call to shell.pack()will make the shell use the preferred size to display its contents. the layout will then ask the text widget to compute its preferred size. the text widget does not know, that the shell will be expanded as well and does not have a fixed size.

调用shell.pack()将使外壳使用首选大小来显示其内容。然后布局将要求文本小部件计算其首选大小。文本小部件不知道外壳也会被扩展并且没有固定大小。

well this problem can be solved if we could give the text widget a maximum size. but it will cause another problem with the grid layout, since we don't want the text widget to be smaller in case that the shell has a minimum size which is bigger than the preferred size of the text widget. i cannot give you a code example of this case, since there is no maximum size, but just imagine the shell be of a fixed size, and the the text widget inside has a maximum size that is smaller.

好吧,如果我们可以给文本小部件一个最大尺寸,这个问题就可以解决。但它会导致网格布局的另一个问题,因为我们不希望文本小部件更小,以防外壳的最小尺寸大于文本小部件的首选尺寸。我不能给你一个这种情况的代码示例,因为没有最大尺寸,但想象一下外壳是固定尺寸,并且里面的文本小部件的最大尺寸更小。

the solution is to limit the preferred size computed by the widget to be of a maximum preferredsize. i didn't find yet a method to set this value, but you can achieve this effect by overriding the computeSize method of the widget:

解决方案是将小部件计算的首选大小限制为最大首选大小。我还没有找到设置此值的方法,但是您可以通过覆盖小部件的 computeSize 方法来实现此效果:

final Text text = new Text(shell, SWT.WRAP | SWT.MULTI | SWT.V_SCROLL) {

    /** The maximum preferred size. */
    private final Point maxSize = new Point(500, 400);

    @Override
    public Point computeSize(final int wHint, final int hHint, final boolean changed) {
        final Point preferredSize = super.computeSize(wHint, hHint, changed);
        return new Point(Math.min(maxSize.x, preferredSize.x), Math.min(maxSize.y, preferredSize.y));
    }

    @Override
    protected void checkSubclass() {
        // noop, allow subclassing, since we know what we are doing
    }
};

now you are still able to increase the shells size by a user drag operation and the widget will still we able to grab more space above the maximum size.

现在您仍然可以通过用户拖动操作来增加外壳的大小,并且小部件仍然可以在最大大小之上获取更多空间。