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

前端之家收集整理的这篇文章主要介绍了如何使用Java检测当前显示?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有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);
原文链接:https://www.f2er.com/java/125732.html

猜你在找的Java相关文章