2013-08-21 54 views
2

我的應用程序用於多屏幕環境。應用程序將其位置關閉並從最後位置開始。
我通過調用frame.getLocation() 獲得該位置如果該框架位於主屏幕上或位於主屏幕的右側,這會給出正值。位於主屏幕左側的屏幕上的幀將爲X獲得負值。
當屏幕配置更改(例如,多個用戶共享一個Citrix帳戶並具有不同的屏幕分辨率)時,問題就會出現。

我現在的問題是要確定存儲的位置是否在屏幕上可見。根據其他一些帖子,我應該使用GraphicsEnvironment來獲得可用屏幕的大小,但我無法獲得不同屏幕的位置。如何知道多屏幕環境中的JFrame是否在屏幕上

實施例:getLocation()給出Point(-250,10)
GraphicsEnvironment給出
Device1的寬度:1920
設備2-寬度:1280

現在,根據屏幕的排序(閹羊在輔助監視器放置在左或在主要的右側),框架可能是可見的,或者不可見。

你能告訴我如何解決這個問題嗎?

非常感謝

回答

3

這是一個小redumentry,但是,如果你想知道的是,如果框架將顯示在屏幕上,你可以計算的臺式機和測試的「虛擬」的範圍看如果幀包含內。

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和檢查每一個反過來...

+0

非常感謝,這幫了我一把! – klib009

0

這可能是幫助他人那裏尋找一個類似的解決方案。

我想知道我的swing應用程序位置的任何部分是否離開屏幕。此方法計算應用程序的區域,並確定它是否全部可見,即使它跨多個屏幕分割。幫助您保存應用程序位置,然後重新啓動它,並且您的顯示配置不同。

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; 
} 
相關問題