且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Java中确定屏幕高度

更新时间:2023-11-14 13:05:46

我用这个

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 上的屏幕大小