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

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

最佳答案

Shell 上监听 SWT.Close

This应该有帮助:

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

它将提示用户验证决定。

关于java - SWT - 修改窗口关闭按钮(红色 X),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16899807/

相关文章:

java - RCP 的多个实例,使用 E4 将 MPerspective/MPart 添加到对话框

java - 表查看器可使用字符串数组项进行编辑

java - 使用java进行服务器端推送

java - 从插件调用代码时出现问题 : "org.eclipse.jface" - Export Deployable Eclipse plug-in

java - View.getView() 返回 null

java - Eclipse:获取空显示

java - 在 setInput() 之前保存 TreeViewer 状态

java - Eclipse 找不到 JFace 中的主类?

java - 为什么Java ArrayList的remove()方法实现不会减少内部数组数据的大小?

适用于Android的JAVA : improper use of "this" constructor causing issues with external class method calls