簡體   English   中英

檢測當前屏幕邊界

[英]Detect current screen bounds

我正在開發一個具有setDecoration(false)的應用程序,我有一個MouseMotionlistener所以我可以移動它,此刻我正在嘗試制作一個最大化按鈕。 在默認監視器上它可以很好地工作,但在第二個監視器上,如果我單擊最大化按鈕,它將最大化到默認屏幕。 如何獲取應用程序當前所在屏幕的X和Y坐標?

IE我在1600x900都有2個顯示器,所以如果應用程序在監視器1上,則X和Y將為0和0,但如果是第二個監視器,則為1600和0。

但我需要它,所以它適用於所有尺寸的顯示器,即1200x800,或者如果顯示器垂直而不是水平。

答案取決於屏幕的定義。 您想要默認的屏幕邊界還是特定的屏幕邊界?

我使用以下(及其變體)來確定單個屏幕的屏幕邊界

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;
}

基本思想是提供屏幕位置並找到與其匹配的屏幕。 我有變化采取ComponentWindow但基本上,它歸結為這個。

MouseEvent ,您可以通過調用MouseEvent.getLocationOnScreen來獲得足夠的屏幕坐標

現在,根據你的問題,聽起來你想知道整個“虛擬”屏幕邊界(我可能是錯的),但我使用這種方法(實際上我用它來動態創建多顯示器壁紙,但那是另一個題)

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;
}

基本上,它只是遍歷所有屏幕設備並將那些區域添加到一起以形成“虛擬”矩形。 您可以使用相同的概念將每個屏幕設備的邊界作為數組返回。

您可以使用它來獲取屏幕尺寸。

Toolkit toolkit =  Toolkit.getDefaultToolkit ();
Dimension dim = toolkit.getScreenSize();
System.out.println("Width of Screen Size is "+dim.width+" pixels");
System.out.println("Height of Screen Size is "+dim.height+" pixels");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM