繁体   English   中英

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

[英]SWT - Modifying Window Close Button (Red X)

当用户使用窗口关闭按钮(红色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);
}
  1. 您能否按下窗口关闭按钮(红色X)以使用上面的代码?
  2. 你能禁用窗口关闭按钮(红色X)吗?

Shell上收听SWT.Close

应该有所帮助:

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();
}

它将提示用户验证决定。

在jface对话框中,无论按下OK或按下CANCEL还是单击(红色x按钮),它都会调用close()方法。

覆盖close方法并检查返回代码(使用getReturnCode ()方法)。

我做的第一件事是覆盖ApplicationWindow中的close()方法

/** the rc from the message dialog */
int rc = -1;

@Override
public boolean close()
{
    MessageDialog messagedialog = new MessageDialog(getShell(),
            "Confirm Exit", null, "Are you sure you want to exit?", 4,
            new String[]
            { "Yes", "No" }, 1);
    messagedialog.setBlockOnOpen(true);
    messagedialog.open();

    rc = messagedialog.getReturnCode();

    /** int to hold the return code from the message dialog */
    if (rc == 0)
    {
        return true;
    } else
    {
        return false;
    }
}

我做的第二件事就是在'X'关闭按钮上听事件

shell.addListener(SWT.Close, new Listener()
{
    @Override
    public void handleEvent(Event event)
    {
        switch (rc)
        {
        case 0: // yes pressed
            event.doit = true;
            break;
        case 1: // no pressed
            event.doit = false;
            break;
        case -1: // escape pressed
            break; // do nothing
        default:
            break;
        }
    }
});

“event.doit”布尔字段确定shell的闭包。 如果该值等于'true' - “let doit”,则关闭shell(即rc = 0)。

如果此值等于1(rc = 1),则shell保持打开状态。

如果按下ESC(即rc = -1),我们什么都不做。 但是如果这是一个要求,我们可以最小化shell,或者根据此事件执行一些其他操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM