当我的摇摆应用程序运行时,我改变了屏幕的大小(例如从 1024x768 到 800x600)。
有什么活动可以让我收到通知吗?
或者,我可以每隔几秒检查一次屏幕大小,但 Toolkit.getScreenSize() 一直告诉我旧值。
更改后如何获得真实的屏幕尺寸?
环境:Linux(在 SuSE ES 11 和 Ubuntu 9.04 上测试)
我感谢您的帮助。
马顿
以下对我有用,但我在 Mac 上,所以我不能肯定它会在 Linux 上工作:
System.out.println(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getWidth() + "x" + GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getHeight());
//Ignore this block, it was simply to give me a chance to change my resolution
Scanner readUserInput=new Scanner(System.in);
System.out.println("Waiting on input, go change your resolution");
String myName=readUserInput.nextLine();
System.out.println(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getWidth() + "x" + GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDisplayMode().getHeight());
输出(实际上是输入)如下:
1440x900
Waiting on input, go change your resolution
blah
1280x960
显然,如果您有多个屏幕设备,则必须更加谨慎。
要计算显示大小,您可以使用(取自GraphicsConfiguration javadoc)
Rectangle virtualBounds = new Rectangle();
GraphicsEnvironment ge = GraphicsEnvironment.
getLocalGraphicsEnvironment();
GraphicsDevice[] gs =
ge.getScreenDevices();
for (int j = 0; j < gs.length; j++) {
GraphicsDevice gd = gs[j];
GraphicsConfiguration[] gc =
gd.getConfigurations();
for (int i=0; i < gc.length; i++) {
virtualBounds =
virtualBounds.union(gc[i].getBounds());
}
}
然后您可以查询的width
和height
。virtualBounds
这也适用于多显示器设置。
据我所知,没有 JDK 支持的方法来检测更改,所以轮询是你唯一的选择,或者使用JNA和本机代码。如果您的顶级窗口最大化,那么调整显示大小也会调整任何最大化窗口的大小,因此您也可以在那里监听更改。有关编写组件侦听器的示例,请参阅此sun 教程。
如果您没有顶级最大化窗口,那么您也可以通过创建隐藏的最大化窗口来达到相同的效果。我不能肯定这是否会奏效,但值得一试。
尺寸 screenSize = Toolkit.getDefaultToolkit().getScreenSize();
f.setSize(screenSize.width, screenSize.height);