This question already has answers here:
How do I determine which monitor a Swing mouse event occurs in?

(4 个回答)


6年前关闭。




我想制作一个对不同显示器表现不同的应用程序(例如,更改 LCD、CRT、五点矩阵等的抗锯齿技术)。 有没有办法发现给定窗口在哪个监视器上? 我只需要它作为一个整数。

最佳答案

是的,这是可能的。我将简化我的答案,根据 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 的初始调用:

关于java - 有没有办法检测窗口在哪个监视器上?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28596967/

10-16 02:26