java - 在 SWT 中创建多个窗口

标签 java swt

我正在尝试创建一个 Window 类,我可以用它来打开多个窗口,它会自动添加一个事件处理程序来监听 Swt.CLOSE事件,并在调用时调用 shell.dispose() 方法。

我的问题是:

  1. 在这种情况下,我需要监听 shell.dispose(),还是只在我的 main 方法中监听 display.dispose()

  2. 我需要在自己的线程中运行每个窗口,还是所有窗口都可以共享同一个 UI 线程?我读过一些关于在打开多个窗口的情况下与事件处理相关的错误行为的报告。

最佳答案

我建议您应该始终有一个单独的 UI 线程,单个 Display 对象在该线程上运行。参见 SWT: single vs. multiple displays甚至 Eclipse documentation on Display强烈建议使用单个 Display 对象:

Applications which are built with SWT will almost always require only a single display. In particular, some platforms which SWT supports will not allow more than one active display.

甚至有几个可用的示例应用程序(例如 this one)演示了 SWT 中的多个 shell。当您想要关闭窗口时调用 shell.dispose() 是可行的方法。

您应该只在关闭整个应用程序时使用 display.dispose(),基本上作为“最后一步”- 参见 this例如,或 this SWT Snippets page 上的一个或几乎任何片段.

编辑

Eclipse 框架本身就是一个可以有多个窗口的应用程序示例 - 它仍然使用单个 Display,具有单个 UI 线程和共享事件系统。 Eclipse documentation on Threading Issues对此有一个基本的解释:

Underneath any GUI application, regardless of its language or UI toolkit, the OS platform detects GUI events and places them in application event queues. [...] It determines which window and application should receive each event and places it in the application's event queue.

关于java - 在 SWT 中创建多个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18822758/

相关文章:

linux - 设置 Window tester pro linux。 SWT错误

java - GridBagLayout 扩展列

java - 从 2 个变量的映射中获取结果

java - Java 内存模型顺序一致性与 Leslie Lamport 定义有何不同?

java - 限制我自己的小部件采用 SWT 中的任何尺寸集

java - 如何在 SWT 中删除鼠标滚轮监听器

java - 尝试写入文档是否会消耗 Cloud Firestore 中的写入操作成本?

java - 加快 Selenium WebDriver 的速度?

java - 如何对表重新排序

java - SWT:将键码转换为字符串