我有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);

dawei

【声明】:淮南站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。