SWT - 修改窗口关闭按钮(红色X)

jkt*_*ter 3 java swt jface

当用户使用窗口关闭按钮(红色X)按钮关闭任何应用程序窗口时.它会导致我的应用程序出现Widget is Disposed问题.当他们使用我提供的关闭应用程序关闭窗口时.一切正常.

@Override
protected void createButtonsForButtonBar(Composite parent) {
   createButton(parent, IDialogConstants.OK_ID, "Close Aplot", true);
}

@Override
protected void okPressed() {
   getShell().setVisible(false);
}
Run Code Online (Sandbox Code Playgroud)
  1. 您能否按下窗口关闭按钮(红色X)以使用上面的代码?
  2. 你能禁用窗口关闭按钮(红色X)吗?

Baz*_*Baz 6

倾听SWT.CloseShell.

应该有所帮助:

public static void main(String[] args)
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.addListener(SWT.Close, new Listener()
    {
        public void handleEvent(Event event)
        {
            int style = SWT.APPLICATION_MODAL | SWT.YES | SWT.NO;
            MessageBox messageBox = new MessageBox(shell, style);
            messageBox.setText("Information");
            messageBox.setMessage("Close the shell?");
            event.doit = messageBox.open() == SWT.YES;
        }
    });

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}
Run Code Online (Sandbox Code Playgroud)

它将提示用户验证决定.