有什么方法可以检测 window 打开的是哪个监视器吗?

Is there any way to detect which monitor a window is on?

我想制作一个针对不同显示器表现不同的应用程序(例如,更改 LCD、CRT、pentile 矩阵等的抗锯齿技术)。 有什么方法可以发现给定的 window 在哪个监视器上吗? 我只需要它作为整数。

是的,这是可能的。我将简化我的答案,根据 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.