Detect which monitor shows the Window

☆樱花仙子☆ 提交于 2019-12-04 15:13:01

问题


I do have main application JFrame window which can include different components. I open a self implemented OnScreenKeyboard when the user select a editable textfield. The OSK is also a JFrame window.

When the user drag the main window to another monitor, the OSK should also be shown on the same monitor. For this i have to detect the monitor the main JFrame is shown.

I try to find a method in

Toolkit.getDefaultToolkit()

but was not able to find someting.

Do you know how i can detect the monitor where a JFrame is shown?

Java-Version 1.4 Windows XP

Thanks


回答1:


Answer, if the solution of all available monitors are the same.

For AWT:

Every Control does have the method getMonitor() from which the screen position get can calculated from like:

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
}

For SWT:

It is just a snip at my origial code. you should ask if return values are not null ans something like this!

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


来源:https://stackoverflow.com/questions/450069/detect-which-monitor-shows-the-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!