保持在主应用程序窗口顶部的最小化 JFrame [英] Minimizable JFrame that stays on top of main application window

查看:32
本文介绍了保持在主应用程序窗口顶部的最小化 JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用会弹出一个对话框.用户通常希望在此对话框和应用程序窗口之间来回切换一段时间.我希望这个对话框保持在顶部,这样它就不会隐藏在主应用程序窗口后面.但同时我希望对话框有一个最小化按钮,以便在一段时间不需要时可以让开.

My apps pops up a dialog. Users usually want to switch back and forth between this dialog and the application window for a period of time. I want this dialog to stay on top, so that it doesn't get hidden behind the main application window. But at the same time I want the dialog to have a minimize button so that it can get out of the way if it's not needed for a while.

这是我尝试过的:

  • 使用无模式的 JDialog - 对话框很好地保持在前面,但它没有最小化按钮,也没有自己的任务栏按钮

  • use a modeless JDialog - the dialog stays in front nicely, but it doesn't not have a minimize button, and it doesn't have its own taskbar button either

使用 JFrame - 对话框现在有一个最小化按钮和它自己的任务栏按钮,但是当主窗口获得焦点时,对话框隐藏在它后面.

use a JFrame - the dialog now has a minimize button and its own taskbar button, but when the main window gets focus the dialog is hidden behind it.

使用 JFrame 并在对话框中添加 WindowListener.windowDeactivated() { this.toFront() }.这样做的问题是 toFront() 也设置了焦点,所以你会得到一个奇怪的焦点闪烁效果.

use a JFrame and add WindowListener.windowDeactivated() { this.toFront() } on the dialog. The problem with this is that toFront() also sets the focus, so that you get a weird focus flickering effect.

使用带有 setAlwaysOnTop 的 JFrame - 这是最接近的解决方案,但现在窗口将位于所有其他应用程序之上,而不仅仅是我的应用程序.

use a JFrame with setAlwaysOnTop - this is the neaerest solution, but now the window will stay on top of all other applications, not just my application.

如果 toFront 只是将 JFrame 带到前面而不改变焦点,那会很容易,但不幸的是,情况并非如此.还有其他方法可以更改 JFrame Z 顺序吗?

It would be easy if toFront just brought the JFrame to the front without changing the focus, but unfortunately that is not the case. Is there another way to change JFrame Z-order?

我突然想到,如果有一种简单的方法可以卷起"JDialog,即最小化它而不是任务栏,那也可以解决我的问题.

edit: It just occurred to me that if there were an easy way to "roll-up" a JDialog, i.e. minimize it but not to the taskbar, that would solve my problem too.

推荐答案

为什么不在框架中监听 focusGained 事件,然后在对话框中使用 setZOrder/toFront 呢?(不确定这是否可行,但值得一试).

Why not listen for the focusGained event in the frame and then use setZOrder/toFront on the dialog at that point? (Not sure this will work but worth a shot).

这篇关于保持在主应用程序窗口顶部的最小化 JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆