检测当前屏幕界限 [英] Detect current screen bounds

查看:182
本文介绍了检测当前屏幕界限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作有 setDecoration(假)应用程序,我有一个的MouseMotionListener 这样我可以移动它四周,此刻我试图做一个最大化按钮。在默认的监控它的作品完美,但第二个显示器上,如果我点击最大化按钮,将最大限度地默认屏幕。我该如何获得X'放大器;屏幕Y坐标的应用目前在?

I am working on an application that has setDecoration(false) and I have a MouseMotionlistener so I can move it around, and at the moment I am trying to make a maximize button. On the default monitor it works perfectly, but on a second monitor if I click the maximize button it will maximize to the default screen. How would I get the X & Y coordinates of the screen the application is currently on?

即。我都在1600x900的2台显示器,所以如果应用程序监视器1时,在X和放大器; Y就应该是0安培; 0,但如果它是一个第二显示器将是1600安培; 0。

I.E. I have 2 monitors both at 1600x900, so if the application is on monitor 1, the X & Y would be 0 & 0, but if it is one the second monitor it would be 1600 & 0.

但我需要它,所以它适用于所有尺寸的显示器,即1200x800,或者显示器定位,而不是水平移动式垂直。

But I need it so it works on all sized monitors i.e. 1200x800, or if the monitors are positioned vertical instead of horizontal.

推荐答案

答案取决于屏幕的定义。你想默认的屏幕边界或特定的屏幕边界?

The answer depends on the definition of screen. Do you want the default screen bounds or a specific screen bounds?

我用下面(放大器,它的变体)来确定单个画面的画面范围

I use the following (& variations of it) to determine the screen bounds for an individual screen

public static GraphicsDevice getGraphicsDeviceAt(Point pos) {
    GraphicsDevice device = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice lstGDs[] = ge.getScreenDevices();
    ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);

    for (GraphicsDevice gd : lstGDs) {
        GraphicsConfiguration gc = gd.getDefaultConfiguration();
        Rectangle screenBounds = gc.getBounds();
        if (screenBounds.contains(pos)) {
            lstDevices.add(gd);
        }
    }

    if (lstDevices.size() == 1) {
        device = lstDevices.get(0);
    }
    return device;
}

public static Rectangle getScreenBoundsAt(Point pos) {
    GraphicsDevice gd = getGraphicsDeviceAt(pos);
    Rectangle bounds = null;

    if (gd != null) {
        bounds = gd.getDefaultConfiguration().getBounds();
    }
    return bounds;
}

的基本思想是提供一种屏幕的位置,寻找匹配它的屏幕。我有变化的需要组件窗口但本质上,它归结为这一点。

The basic idea is to provide a screen location and find the screen that matches it. I have variations the take a Component or Window but essentially, it boils down to this.

的MouseEvent ,可以获取屏幕与呼叫协调只需足以<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseEvent.html#getLocationOnScreen%28%29\"相对=nofollow> MouseEvent.getLocationOnScreen

From a MouseEvent, you can obtain the screen coordinates simply enough with a call to MouseEvent.getLocationOnScreen

现在,从你的问题,这听起来像你想知道整个虚拟屏幕边界(我可能是错的),但我用这个方法(其实我用它来即时创建多显示器壁纸,但这是另外一个问题)

Now, from your question, it sounds like you want to know the entire "virtual" screen bounds (I might be wrong), but I use this method (actually I use it to create multi-monitor wallpapers on the fly, but that's another question)

public static Rectangle getVirtualScreenBounds() {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice lstGDs[] = ge.getScreenDevices();

    Rectangle bounds = new Rectangle();
    for (GraphicsDevice gd : lstGDs) {
        bounds.add(gd.getDefaultConfiguration().getBounds());
    }
    return bounds;
}

基本上,它只是走所有的屏幕的设备和添加的有地区在一起以形成一个虚拟的矩形。可以使用同样的概念,返回每个屏幕设备的边界作为数组代替

Basically, it just walks all the screen devices and add's there regions together to form a "virtual" rectangle. You could use the same concept to return the bounds of each screen device as an array instead.

这篇关于检测当前屏幕界限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆