Determine height of screen in Java(在Java中确定屏幕高度)
问题描述
我的 JFrame 使用以下命令处于全屏模式:
I have my JFrame in full screen mode using the following:
setExtendedState(JFrame.MAXIMIZED_BOTH);
setUndecorated(true);
我想知道高度.请注意, Toolkit.getDefaultToolkit().getScreenSize() 不起作用,因为我在 Mac 上,实际高度应不包括屏幕顶部 Mac 栏的高度.
And I want to know the height. Note that Toolkit.getDefaultToolkit().getScreenSize() does not work because I'm on a Mac and the real height should exclude the height of the Mac bar thing at the top of the screen.
以 Windows 为例,高度应不包括开始栏.因此,我想知道我拥有的窗口空间的真实高度.
And in the case of Windows, for example, the height should exclude the start bar. Hence, I want to know the true height of the window space I have.
推荐答案
我用这个
public static Rectangle getScreenViewableBounds() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
Rectangle bounds = new Rectangle(0, 0, 0, 0);
if (gd != null) {
GraphicsConfiguration gc = gd.getDefaultConfiguration();
bounds = gc.getBounds();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
bounds.x += insets.left;
bounds.y += insets.top;
bounds.width -= (insets.left + insets.right);
bounds.height -= (insets.top + insets.bottom);
}
return bounds;
}
确定安全"的屏幕边界.这会考虑到屏幕插入并产生一个安全"可视区域的矩形......
To determine the "safe" screen bounds. This takes into consideration the screen insets and produces a rectangle of a "safe" viewable area...
更新
经过一些测试,我很满意(就我有多个屏幕的 Windows 而言)GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds() 似乎为默认监视器返回相同的结果.前面提到的方法的好处是,它可以用来确定任何设备的安全"边界
After a little testing, I'm satisifed (as far as I have Windows with multiple screens) that GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds() seems to return the same results for the default monitor. The benifit of the previous mention method, is it could be used to determine the "safe" bounds for any device
感谢 Java - Mac 上的屏幕大小
这篇关于在Java中确定屏幕高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在Java中确定屏幕高度
基础教程推荐
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 存储 20 位数字的数据类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
