如何让 Swing 应用程序感知屏幕尺寸变化?
当我的 swing 应用程序运行时,我更改屏幕尺寸(例如从 1024x768 更改为 800x600)。
我可以收听任何活动以获得有关此事件的通知吗?
或者,我可以每隔几秒检查一次屏幕尺寸,但 Toolkit.getScreenSize() 会不断告诉我旧值。
更改后如何获得真实屏幕尺寸?
环境:Linux(在 SuSE ES 11 和 Ubuntu 9.04 上测试)
感谢您的帮助。
马顿
while my swing app is running I change the size of the screen (e.g. from 1024x768 to 800x600).
Is there any event I can listen to to be notified about this?
Alternatively I could check the screen size in every couple of second, but the Toolkit.getScreenSize() keeps telling me the old value.
How could I get the real screen size after the change?
Environment: Linux (tested on SuSE ES 11 and Ubuntu 9.04)
I appreciate your help.
Marton
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下内容对我有用,但我在 Mac 上,所以我不能肯定地说它会在 Linux 上工作:
输出(实际上是输入)如下:
显然,如果您有多个屏幕设备,则必须更加谨慎。
The following worked for me, but I'm on a Mac, so I can't say for sure that it will work on Linux:
The output (blah is actually input) was as follows:
Obviously if you have multiple screen devices, you will have to exercise greater caution.
要计算显示大小,您可以使用(取自 GraphicsConfiguration javadoc)
然后您可以查询
virtualBounds
的宽度
和高度
。这也适用于多显示器设置。
据我所知,没有 JDK 支持的方法来检测更改,因此轮询是您唯一的选择,或者使用 JNA 和本机代码。如果您的顶级窗口最大化,则调整显示大小也会调整任何最大化窗口的大小,因此您也可以监听那里的更改。有关编写组件侦听器的示例,请参阅此 sun 教程 。
如果您没有顶级最大化窗口,那么您也可以通过创建隐藏的最大化窗口来达到相同的效果。我不能确定这是否有效,但值得尝试。
To compute the display size, you can use (taken from GraphicsConfiguration javadoc)
You can then query the
width
andheight
ofvirtualBounds
.This will also work in multi-monitor setups.
As far as I know, there is no JDK supported method to detect changes, so polling is your only bet, or use JNA and native code. If your top-level window is maximized, then resizing the display will also resize any maximized windows, so you could listen for changes there also. See this sun tutorial for examples of writing component listeners.
If you don't have a top-level maximized window, then you may also be able to achieve the same effect by creating a hidden maximized window. I can't say for sure if this will work, but it is worth trying.
尺寸 screenSize = Toolkit.getDefaultToolkit().getScreenSize();
f.setSize(screenSize.width, screenSize.height);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
f.setSize(screenSize.width, screenSize.height);