如何让swing应用知道屏幕尺寸的变化? [英] How to make a swing app aware of screen size change?

查看:242
本文介绍了如何让swing应用知道屏幕尺寸的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的swing应用程序运行时,我更改了屏幕的大小(例如,从1024x768到800x600)。

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?

或者我可以在几秒钟内查看屏幕大小,但Toolkit.getScreenSize()会一直告诉我旧值。

如何在更改后获得真实的屏幕大小?

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?

环境:Linux(在SuSE ES 11和Ubuntu 9.04上测试)

Environment: Linux (tested on SuSE ES 11 and Ubuntu 9.04)

感谢您的帮助。

Marton

I appreciate your help.
Marton

推荐答案

以下对我有用,但我在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:

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());

输出(实际上是输入)如下:

The output (blah is actually input) was as follows:

1440x900
Waiting on input, go change your resolution
blah
1280x960

显然,如果你有多个屏幕设备,你将不得不更加谨慎。

Obviously if you have multiple screen devices, you will have to exercise greater caution.

这篇关于如何让swing应用知道屏幕尺寸的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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