我希望制作一个应用程序,在不同的显示器上表现出不同的行为(例如,针对液晶屏、CRT、pentile矩阵等更改抗锯齿技术)。有没有办法发现给定窗口所在的显示器? 我只需要这个作为整数。
我希望制作一个应用程序,在不同的显示器上表现出不同的行为(例如,针对液晶屏、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()
的初始调用,它可能返回 null:
获取与此组件相关联的 GraphicsConfiguration。如果此组件未分配特定的 GraphicsConfiguration,则返回 Component 对象的顶级容器的 GraphicsConfiguration。如果已创建组件但尚未添加到容器中,则此方法返回 null。