0

我的 JFrame 使用以下命令处于全屏模式:

setExtendedState(JFrame.MAXIMIZED_BOTH);
setUndecorated(true);

我想知道高度。请注意, Toolkit.getDefaultToolkit().getScreenSize() 不起作用,因为我在 Mac 上,实际高度应不包括屏幕顶部 Mac 栏的高度。

例如,在 Windows 的情况下,高度应不包括开始栏。因此,我想知道我拥有的窗口空间的真实高度。

4

2 回答 2

3

我用这个

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;

}

确定“安全”的屏幕边界。这考虑了屏幕插图并产生一个“安全”可视区域的矩形......

更新

经过一些测试,我很满意(就我有多个屏幕的 Windows 而言),GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds()默认监视器似乎返回了相同的结果。前面提到的方法的好处是,它可以用来确定任何设备的“安全”边界

归功于Java - Mac 上的屏幕大小

于 2013-01-07T02:38:45.090 回答
0

frame.getContentPane().getHeight();

当你使用这个方法时,你得到的是 JFrame 的高度,而不是屏幕的高度。它还排除了边界高度。

于 2013-01-07T02:33:29.273 回答