2009-01-16 55 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; 
    } 
相关问题