<分区>
我想制作一个针对不同显示器表现不同的应用程序(例如,更改 LCD、CRT、pentile 矩阵等的抗锯齿技术)。 有什么方法可以发现给定窗口在哪个监视器上吗?我只需要它作为一个整数。
<分区>
我想制作一个针对不同显示器表现不同的应用程序(例如,更改 LCD、CRT、pentile 矩阵等的抗锯齿技术)。 有什么方法可以发现给定窗口在哪个监视器上吗?我只需要它作为一个整数。
最佳答案
是的,这是可能的。我会将我的答案简化为根据 Window
的顶部 x,y 位置获取监视器(因此,如果您将框架的上半部分从任何监视器上移开,这当然会失败,通常这代码并不健壮,但可以作为入门示例)。
public GraphicsDevice getMonitorWindowIsOn(Window window)
{
// First, grab the x,y position of the Window object
GraphicsConfiguration windowGraphicsConfig = window.getGraphicsConfiguration();
if (windowGraphicsConfig == null)
{
return null; // Should probably be handled better
}
Rectangle windowBounds = windowGraphicsConfig.getBounds();
for (GraphicsDevice gd : ge.getScreenDevices()) {
Rectangle monitorBounds = gd.getDefaultConfiguration().getBounds();
if (monitorBounds.contains(windowBounds.x, windowBounds.y))
{
return gd;
}
}
// um, return null I guess, should make this default better though,
// maybe to the default screen device, except, I am sure if no monitors are
// connected that may be null as well.
return null;
}
关于 window.getGraphicsConfiguration()
的初始调用,it can return null :
Gets the GraphicsConfiguration associated with this Component. If the Component has not been assigned a specific GraphicsConfiguration, the GraphicsConfiguration of the Component object's top-level container is returned. If the Component has been created, but not yet added to a Container, this method returns null.
关于java - 有什么方法可以检测窗口在哪个监视器上吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28596967/