2009-01-16 43 views
3

我的主應用程序JFrame窗口可以包含不同的組件。當用戶選擇一個可編輯的文本框時,我打開一個自我實現的OnScreenKeyboard。 OSK也是一個JFrame窗口。檢測哪個監視器顯示窗口

當用戶將主窗口拖到另一臺顯示器上時,OSK也應顯示在同一顯示器上。爲此,我必須檢測顯示主JFrame的監視器。

我試圖找到一種方法,

Toolkit.getDefaultToolkit() 

但無法找到成才。

你知道我可以如何檢測顯示JFrame的顯示器嗎?

了Java的版本1.4 的Windows XP

感謝

回答

4

回答,如果所有可用的顯示器的解決方案都是一樣的。

對於AWT

每個控制確實有該方法getMonitor(),從該屏幕位置獲得可以從等計算:

Monitor widgetMonitor = mTextWidget.getMonitor(); 
Rectangle monitorRect = widgetMonitor.getBounds(); 

if(monitorRect.x < 0){ 
    // shown in left monitor, starting from the main monitor 
} 

if(monitorRect.x > monitorRect.width){ 
    // shown in right monitor, starting from the main monitor 
} 

對於SWT

它只是我的原始代碼中的一個片段。你應該問一下,如果返回值不爲空,就像這樣!

int monitorWidth = 0; 
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
    GraphicsDevice[] screenDevices = ge.getScreenDevices(); 
    if(screenDevices.length > 0){ 
     monitorWidth = screenDevices[0].getDisplayMode().getWidth(); 
    } 


    Point ownerLocationOnScreen = owner.getLocationOnScreen(); 

    int screenMovingX = 0; 
    if(ownerLocationOnScreen.x < 0){ 
     screenMovingX = -monitorWidth; 
    } 
    if(ownerLocationOnScreen.x > monitorWidth){ 
     screenMovingX = monitorWidth; 
    }