Java Swing - 检测 JFrame 显示在哪个显示器上

Java Swing - Detect which display a JFrame is displayed on

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

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

我知道从这里我可以自己设置设备,让用户 select 在菜单中使用哪个,等等。我的目标是检测应用程序当前显示在这些 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。我知道直接定位 window 可能会有黑客攻击(如上文 link 所述),但我想知道是否有 easier/more 优雅的解决方案。

GraphicsDevice device = jframe.getGraphicsConfiguration().getDevice()