加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何使用Java检测当前显示?

发布时间:2020-12-14 16:20:32 所属栏目:Java 来源:网络整理
导读:我有2个显示器连接,所以我可以在主显示器或辅助显示器上启动我的 Java应用程序. 问题是:我如何知道哪个显示包含我的应用程序窗口,即有没有办法用Java检测当前显示? 解决方法 java.awt.Window是所有顶级窗口(Frame,JFrame,Dialog等)的基类,它包含返回窗口正
我有2个显示器连接,所以我可以在主显示器或辅助显示器上启动我的 Java应用程序.

问题是:我如何知道哪个显示包含我的应用程序窗口,即有没有办法用Java检测当前显示?

解决方法

java.awt.Window是所有顶级窗口(Frame,JFrame,Dialog等)的基类,它包含返回窗口正在使用的 GraphicsConfiguration的getGraphicsConfiguration()方法. GraphicsConfiguration具有getGraphicsDevice()方法,它返回GraphicsConfiguration所属的 GraphicsDevice.然后,您可以使用 GraphicsEnvironment类来对系统中的所有GraphicsDevices进行测试,并查看该窗口属于哪一个.
Window myWindow = ....
// ...
GraphicsConfiguration config = myWindow.getGraphicsConfiguration();
GraphicsDevice myScreen = config.getDevice();
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
// AFAIK - there are no guarantees that screen devices are in order... 
// but they have been on every system I've used.
GraphicsDevice[] allScreens = env.getScreenDevices();
int myScreenIndex = -1;
for (int i = 0; i < allScreens.length; i++) {
    if (allScreens[i].equals(myScreen))
    {
        myScreenIndex = i;
        break;
    }
}
System.out.println("window is on screen" + myScreenIndex);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读