Detect which monitor shows the Window(检测哪个显示器显示窗口)
问题描述
我确实有可以包含不同组件的主应用程序 JFrame 窗口.当用户选择可编辑的文本字段时,我会打开一个自行实现的 OnScreenKeyboard.OSK 也是一个 JFrame 窗口.
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.
当用户将主窗口拖到另一个监视器上时,OSK 也应该显示在同一监视器上.为此,我必须检测显示主要 JFrame 的监视器.
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.
我尝试在
Toolkit.getDefaultToolkit()
但找不到东西.
你知道我如何检测显示 JFrame 的监视器吗?
Do you know how i can detect the monitor where a JFrame is shown?
Java-版本 1.4视窗XP
Java-Version 1.4 Windows XP
谢谢
推荐答案
回答,如果所有可用监视器的解决方案都相同.
Answer, if the solution of all available monitors are the same.
对于AWT:
每个控件都有 getMonitor() 方法,可以从中计算屏幕位置,如下所示:
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
}
对于 SWT:
这只是我原始代码的一个片段.您应该询问返回值是否不为 null 之类的!
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;
}
这篇关于检测哪个显示器显示窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检测哪个显示器显示窗口
基础教程推荐
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01