2

我的应用程序用于多屏幕环境。应用程序在关闭时存储它的位置并从最后一个位置开始。
如果框架在主屏幕上或者它在主屏幕的右侧,我通过调用frame.getLocation() 这给我一个正值。位于主屏幕左侧屏幕上的帧的 X 值为负。
当屏幕配置更改时(例如,多个用户共享一个 Citrix-Account 并具有不同的屏幕分辨率),就会出现问题。

我现在的问题是确定存储的位置是否在屏幕上可见。根据其他一些帖子,我应该使用GraphicsEnvironment来获取可用屏幕的大小,但我无法获取不同屏幕的位置。

示例:getLocation()给出Point(-250,10)
GraphicsEnvironmentDevice1
-Width: 1920
Device2-Width: 1280

现在,根据屏幕的顺序(辅助监视器是放在主监视器的左侧还是右侧),框架可能是可见的,或者不是。

你能告诉我如何解决这个问题吗?

非常感谢

4

2 回答 2

6

这有点简化,但是,如果您只想知道框架是否在屏幕上可见,您可以计算桌面的“虚拟”边界并测试框架是否包含在其中。

public class ScreenCheck {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setBounds(-200, -200, 200, 200);
        Rectangle virtualBounds = getVirtualBounds();

        System.out.println(virtualBounds.contains(frame.getBounds()));

    }

    public static Rectangle getVirtualBounds() {
        Rectangle bounds = new Rectangle(0, 0, 0, 0);
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice lstGDs[] = ge.getScreenDevices();
        for (GraphicsDevice gd : lstGDs) {
            bounds.add(gd.getDefaultConfiguration().getBounds());
        }
        return bounds;
    }
}

现在,这使用了Rectangle框架的 ,但您可以使用它的位置来代替。

同样,您可以单独使用每个GraphicsDevice并依次检查每个...

于 2013-08-21T07:21:30.770 回答
5

这可能对寻找类似解决方案的其他人有所帮助。

我想知道我的摇摆应用程序位置是否有任何部分不在屏幕上。此方法计算应用程序的区域并确定是否所有区域都可见,即使它被拆分为多个屏幕。当您保存应用程序位置然后重新启动它并且您的显示配置不同时会有所帮助。

public static boolean isClipped(Rectangle rec) {

    boolean isClipped = false;
    int recArea = rec.width * rec.height;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice sd[] = ge.getScreenDevices();
    Rectangle bounds;
    int boundsArea = 0;

    for (GraphicsDevice gd : sd) {
        bounds = gd.getDefaultConfiguration().getBounds();
        if (bounds.intersects(rec)) {
            bounds = bounds.intersection(rec);
            boundsArea = boundsArea + (bounds.width * bounds.height);
        }
    }
    if (boundsArea != recArea) {
        isClipped = true;
    }
    return isClipped;
}
于 2016-09-29T17:33:33.803 回答