gpt4 book ai didi

shell - SWT - 可靠地知道 shell 用户是否切换到另一个 shell 的事件

转载 作者:行者123 更新时间:2023-12-04 20:15:23 28 4
gpt4 key购买 nike

我需要知道,当用户切换到另一个 shell 时,通过单击它。

我试过 shellListener.shellDeactivated()但是当 shell 失去对它自己的控件的焦点时,会触发此事件,这意味着单击事件 shell 中的控件时。

这不是预期的行为,因为我需要知道何时激活另一个 Shell 而不是我的 Shell。

有任何想法吗?

最佳答案

假设您有三个 shell 。一个主 shell 和两个可以从主 shell 打开的 shell 。

因此,当另一个 shell 被激活时,应该通知主 shell。
您可以添加一个等待特定事件类型的监听器(在主 shell 中):

    shell.addListener(SWT.Show, new Listener() {

@Override
public void handleEvent(Event e) {
System.out.println("activated: " + e.text);
}
});

当其他炮弹被激活时,它们应该触发( notify )这个事件。为此,您向其他两个 shell 添加了一个 shell 监听器,然后 fire方法中的事件 shellActivated()o.notifyListeners(); .
    shell.addShellListener(new ShellAdapter() {
@Override
public void shellActivated(ShellEvent e) {
Shell shellSrc = (Shell) e.getSource();
Display display = shellSrc.getDisplay();

Event event = new Event();
event.type = SWT.Show;
event.text = "other shell 1";

Shell[] shells = display.getShells();
for(Shell o : shells) {
o.notifyListeners(SWT.Show, event);
}
}
});

当您激活其他两个 shell 之一时,主 shell 将收到事件类型 SWT.Show 的通知。 .

关于shell - SWT - 可靠地知道 shell 用户是否切换到另一个 shell 的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13397404/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com