【问题标题】:Detect which monitor shows the Window检测哪个显示器显示窗口
【发布时间】:2009-01-16 10:56:08
【问题描述】:
我确实有可以包含不同组件的主应用程序 JFrame 窗口。当用户选择可编辑的文本字段时,我会打开一个自行实现的 OnScreenKeyboard。 OSK 也是一个 JFrame 窗口。
当用户将主窗口拖到另一台显示器上时,OSK 也应该显示在同一台显示器上。为此,我必须检测显示主要 JFrame 的监视器。
我尝试在
中找到一个方法
Toolkit.getDefaultToolkit()
但找不到东西。
你知道我如何检测显示 JFrame 的监视器吗?
Java-版本 1.4
视窗XP
谢谢
【问题讨论】:
标签:
java
windows
jframe
toolkit
multiple-monitors
【解决方案1】:
回答,如果所有可用监视器的解决方案都相同。
对于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;
}