是否可以知道 JFrame 是否完全可见?
我只是在尝试 JFrames,想知道我的应用程序窗口是否完全可见或被其他应用程序窗口遮挡。另一个应用程序窗口可以是本机应用程序。如果可能的话,我可以检索不可见区域的大小和位置吗?
I'm just experimenting with JFrames and would like to know if my application window is fully visible or obscured by some other application window. The other application window can be a native app. If this is possible, can I retrieve the size and position of the area that is not visible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个非常令人讨厌的方法,这是我能想到的唯一方法(并且不会推荐):
JFrame
的玻璃窗格设置为完全红色,并且显示玻璃板(暂时)。Robot
类对JFrame
当前所在的屏幕坐标中的所有像素(或多个像素)进行采样。JFrame
前面可能没有任何内容。替代方案(更好的解决方案)
只需在
JFrame
上调用toFront()
即可将其置于最前面并确保它具有焦点。Here is a really nasty approach, which is the only approach I can think of (and wouldn't recommend):
JFrame
's glass pane to be completely red and show the glass pane (temporarily).Robot
class to sample all pixels (or a number of pixels) from the screen coordinates where yourJFrame
is currently positioned.JFrame
.Alternative (nicer solution)
Simply call
toFront()
on yourJFrame
to bring it to the front and ensure it has focus.不,这是不可能的,因为您的 Java 应用程序只知道其窗口内容,而不知道其他内容。窗口装饰和屏幕的其余部分由操作系统管理。在类似 Unix 的操作系统(例如 Linux)中,窗口管理器甚至是一个完全不同的用户空间进程。另一个本机应用程序也将在另一个用户空间进程中运行。
我可以问你为什么想要这个吗?如果您询问刷新/重画问题,那么您无需执行任何操作。希望您的操作系统足够智能,当它再次可见时,它会要求您的应用程序重新绘制自身。
No it is not possible, since your Java application knows only its window contents, and nothing else. The window decorations and the rest of the screen are managed by the operating system. In a Unix like OS such as Linux the window manager is even a completely different user-space process. The other native app also will run in another user-space process as well.
May I ask why you want this? If you are asking about refresh/repaint issues, then there is nothing you need to do. Your OS is hopefully smart enough and it will ask your application to repaint itself when it becomes visible again.