1

I know I can use this code to tell when the shell is closed,

shell.addShellListener(new ShellAdapter()
{
    @Override
    public void shellClosed(ShellEvent e)
    {
        System.out.println("closed");
    }
}

But the ShellEvent object doesn't tell me whether the Shell was closed programmatically or when the user clicked on the X button.

Is there a way to tell?

4

1 回答 1

2

我花了一些时间来区分 CloseShellEvent是由User还是System生成的。

在检查了ShelEvent这两种情况之后,唯一具有不同值的变量通过 ObjectGraphShellEventcaptureChangedDisplay其范围为的类中default

下面的代码应该可以帮助您找到ShellEvent

shell.addShellListener(new ShellAdapter() {
        @Override
        public void shellClosed(ShellEvent e) {

                Field f = Display.class.getDeclaredField("captureChanged");
                f.setAccessible(true);
                System.out.println("captureChanged = " + f.get(e.display)); //true = If User triggered the Event
                System.out.println("closed");
        }
    });
于 2013-07-14T10:33:59.040 回答