【问题标题】:Java Swing - Detect which display a JFrame is displayed onJava Swing - 检测 JFrame 显示在哪个显示器上
【发布时间】:2015-07-30 07:32:43
【问题描述】:

我从 this question 了解到,您可以使用以下代码在 swing 应用程序中以 GraphicsDevice[] 的形式获取当前显示:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gd = ge.getScreenDevices();

我知道从这里我可以自己设置设备,让用户在菜单中选择要使用的设备,等等。我的目标是检测应用程序当前显示在哪些 GraphicsDevices 中,所以我可以默认为全屏在当前的 GraphicsDevice 中,而不会打扰用户。

我正在使用如下代码来全屏显示我的应用程序:

JFrame frame = new JFrame();

// ... when I want to fullscreen:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
if (gd.isFullScreenSupported()){
    frame.dispose();
    frame.setUndecorated(true);
    frame.setAlwaysOnTop(true);
    gd.setFullScreenWindow(frame);
    frame.setVisible(true);
}

我在 GraphicsDevice 或 GraphicsEnvironment API 中找不到任何东西来获取显示 JFrame 的当前 GraphicsDevice。我知道直接定位窗口可能会有黑客攻击(如上面的链接中所述),但我想知道是否有更简单/更优雅的解决方案。

【问题讨论】:

  • 一般我只用GraphicsDevices的GraphicsConfigurations来测试Window是否在设备的范围内,对于example
  • 太棒了,所以我可以测量窗口与最大区域重叠的设备?
  • 是的,示例 (getGraphicsDevice(Component)) 也是这样做的 ;)
  • 查看 Santhosh 的回答。可以完美运行,就像您编写的代码示例一样(也可以),但是当它已经在 API 中时,为什么要重新发明轮子呢?
  • 可能的原因是它要么没有做我想要的,要么没有返回我想要或预期的结果

标签: java swing jframe fullscreen


【解决方案1】:
GraphicsDevice device = jframe.getGraphicsConfiguration().getDevice()

【讨论】:

  • 哇,超级简单!我以前怎么没看到
  • 我使用 Intellij Idea。 GraphicsDevice device = frame. 神奇地完成给我答案
  • 我用过 Intellij 的 Android Studio,IDE 有很多非常不错的功能,很高兴认识一个新功能!
猜你喜欢
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多