在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 上的屏幕大小
相关文章