如何让 Swing 应用程序感知屏幕尺寸变化?

发布于 2024-09-05 03:38:25 字数 251 浏览 4 评论 0原文

当我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

提笔落墨 2024-09-12 03:38:25

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

显然,如果您有多个屏幕设备,则必须更加谨慎。

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.

梦太阳 2024-09-12 03:38:25

要计算显示大小,您可以使用(取自 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());
      }
  } 

然后您可以查询virtualBounds宽度高度

这也适用于多显示器设置。

据我所知,没有 JDK 支持的方法来检测更改,因此轮询是您唯一的选择,或者使用 JNA 和本机代码。如果您的顶级窗口最大化,则调整显示大小也会调整任何最大化窗口的大小,因此您也可以监听那里的更改。有关编写组件侦听器的示例,请参阅此 sun 教程

如果您没有顶级最大化窗口,那么您也可以通过创建隐藏的最大化窗口来达到相同的效果。我不能确定这是否有效,但值得尝试。

To compute the display size, you can use (taken from 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());
      }
  } 

You can then query the width and height of virtualBounds.

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.

暖阳 2024-09-12 03:38:25

尺寸 screenSize = Toolkit.getDefaultToolkit().getScreenSize();

f.setSize(screenSize.width, screenSize.height);

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

f.setSize(screenSize.width, screenSize.height);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文