SWT Java:如何防止窗口调整大小?

10

我想禁用窗口的大小调整。有什么建议吗?


可能是Non resizable window with JFace的重复问题。 - Bala R
3个回答

29
您可以使用双参数构造函数来指定Shell样式位。默认的样式位是SWT.SHELL_TRIM:
public static final int SHELL_TRIM = CLOSE | TITLE | MIN | MAX | RESIZE;

你实际上想要排除RESIZE位。如果你正在创建自己的Shell

final Shell shell = new Shell(parentShell, SWT.SHELL_TRIM & (~SWT.RESIZE));

如果你正在扩展 Dialog,你可以通过覆盖 getShellStyle 方法来影响外壳样式位:

@Override
protected int getShellStyle()
{
    return super.getShellStyle() & (~SWT.RESIZE);
}

5
你可以在声明 shell 时控制家具。我认为这个示例可以实现你想要的功能;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class FixedWindow {
    public static void main(String[] args) {
        Display display = new Display();

        //final Shell shell = new Shell(display); //defaults
        //final Shell shell = new Shell(display, SWT.CLOSE | SWT.TITLE | SWT.MIN | SWT.MAX); //can be maximised
        final Shell shell = new Shell(display, SWT.CLOSE | SWT.TITLE | SWT.MIN ); // fixed but can be minimised
        //final Shell shell = new Shell(display,  SWT.TITLE ); // fixed, uncloseable, unminimisable can only be removed by OS killing JVM.

        Rectangle boundRect = new Rectangle(0, 0, 1024, 768);
        shell.setBounds(boundRect);
        Rectangle boundInternal = shell.getClientArea();

        shell.setText("Fixed size SWT Window.");

        shell.open();

        final Text text = new Text(shell, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);

        text.setEditable(true);
        text.setEnabled(true);
        text.setText("Oh help!");
        text.setBounds(boundInternal);


        while (!shell.isDisposed()) {

            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

谢谢,其实我已经在此期间解决了它,方法是添加:"new Shell(display, SWT.CLOSE | SWT.TITLE)",而您的答案也是这样做的,但还有MIN。 - alhcr

-3

我不太确定,但我认为你可以像这样简单地删除 SWT.Resize 事件:

shell.addListener (SWT.Resize, new Listener () {
    public void handleEvent (Event e)
    {
       return;
    }
});

4
在实践中,这并不完全有效 - 调整大小侦听器在窗口调整大小之后而不是之前被触发,因此将 e.doit 设置为 false 没有效果。 在一些平台上,您可以尝试将 shell 的大小设置回之前的大小,但在一些平台上会看起来很奇怪(特别是使用线框调整大小或允许在触发事件之前进行相当大量调整大小的平台)。在其他平台上,当您在调整大小侦听器内调整 shell 的大小时,实际上会得到一个无限循环的事件(除非您设置正在更改大小的标志)。 - Edward Thomson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接