代码之家  ›  专栏  ›  技术社区  ›  Markus Lausberg

检测显示窗口的监视器

  •  3
  • Markus Lausberg  · 技术社区  · 17 年前

    我有主应用程序JFrame窗口,它可以包含不同的组件。当用户选择一个可编辑的文本字段时,我打开一个自我实现的屏幕。OSK也是一个JFrame窗口。

    当用户将主窗口拖动到另一个监视器时,OSK也应显示在同一个监视器上。为此,我必须检测显示器的主框架显示。

    Toolkit.getDefaultToolkit()
    

    但是找不到什么东西。

    Java版本1.4 视窗XP

    1 回复  |  直到 17 年前
        1
  •  5
  •   Markus Lausberg    17 年前

    回答:如果所有可用监控器的解决方案相同。

    对于 AWT :

    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

    这只是对我的原始代码的一个剪贴。您应该询问返回值是否不为null,或者类似这样的问题!

        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;
        }
    
    推荐文章