解决方法
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);