How to use java to detect the current display?

I have 2 monitors connected, so I can launch my java application on the primary or secondary monitor

The question is: how do I know which display contains my application window, that is, is there a way to detect the current display in Java?

Solution

java. awt. Window is the base class of all top-level windows (frame, JFrame, dialog, etc.), which contains the getgraphicsconfiguration() method that returns the graphicsconfiguration being used by the window Graphicsconfiguration has a getgraphicsdevice () method, which returns the graphicsdevice to which graphicsconfiguration belongs Then, you can use the graphicsenvironment class to test all graphicsdevices in the system and see which window belongs to

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);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>