我有主要的应用程序JFrame窗口,可以包含不同的组件。 当用户select一个可编辑的文本字段时,我打开一个自我实现的OnScreenKeyboard。 OSK也是一个JFrame窗口。
当用户将主窗口拖到另一台监视器上时,OSK也应显示在同一监视器上。 为此,我必须检测显示主JFrame的显示器。
我试图find一种方法
Toolkit.getDefaultToolkit()
但没能find。
你知道我怎么能检测显示JFrame显示器?
Java版本1.4 Windows XP
谢谢
回答,如果所有可用的显示器的解决方案是相同的。
对于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 :
这只是我的原始代码片段。 你应该问问,如果返回值不是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; }