我已经阅读了有关使用 JDialog 包装 JFXPanel 以使用 JDialog 的 alwaysOnTop 方法的信息。它有效,但我对这个黑客有一个小问题。

由于我使用这种技术为我的主应用程序(弹出窗口等)制作辅助窗口,因此我需要将它们设置在主窗口的顶部。如果我使用 wrapper hack 来执行此操作,则顶部面板“始终位于”所有内容(包括其他应用程序)的顶部。

有没有办法将辅助屏幕仅放在我的应用程序之上?我不喜欢可以在我的应用程序的主窗口和辅助窗口之间拖动另一个应用程序的事实。

请您参考如下方法:

我建议不要使用 JDialogJFXPanel , 但仅使用 JavaFX Stages .

将您的辅助屏幕设为 Stage并调用 secondaryStage.initOwner(primaryStage)在你展示第二阶段之前。

从舞台文档中:

A stage can optionally have an owner Window. When a window is a stage's owner, it is said to be the parent of that stage . . . A stage will always be on top of its parent window.



我相信设置辅助阶段的所有者,正确地满足了您“仅在我的应用程序之上的辅助屏幕”的要求。

更新:对评论中其他问题的回答

I don't want there to be any interaction with the main stage once the secondary is open (the secondary window must be closed to allow interaction again).



要阻止初级阶段的输入,在显示次级阶段之前,请调用: secondaryStage.initModality(Modality.WINDOW_MODAL) .

您可以使用 APPLICATION_MODAL而不是 WINDOW_MODAL如果您希望阻止对任何其他应用程序窗口的所有输入 - 使用哪一个取决于您想要的用户体验。

There's nothing obvious in the api that would center the secondary screen on the main screen. It just always centers on the monitor, no matter where the main screen is.



这部分问题与 Center location of stage 重复。 .副本的答案有用于执行子窗口居中的示例代码。

没错,公共(public) api 中没有关于相对于父窗口定位子窗口的内容。我已经为此功能提交了功能请求 Add helper methods for positioning popups relative to nodes ,但从 JavaFX 2.2 开始,该功能请求尚未实现。

有一个 sample project我创建了子对话框的相对定位,这可能很有用。

只是为了在父级中居中,您最好在显示子级之前查询舞台位置和宽度,然后在显示子级时适本地设置子级的 x 和 y 坐标。这一切都可以基于 x , ywidth window 的属性。

JavaFX UI controls sandbox 中还有一个对话项目它可能会提供您需要的一些功能,因此您无需自己编写代码。

随着时间的推移,您请求的所有功能都可能最终出现在 JavaFX 核心平台中,但我认为 JavaFX 2.2 还没有完全实现。


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!