如何在OSX上使用Java进行全屏显示

Sim*_*ews 9 java macos awt fullscreen

我一直在尝试并且未能在OSX系统的主显示器上使用java全屏模式.无论我尝试过什么,我似乎都无法摆脱显示屏顶部的"苹果"菜单栏.我真的需要在整个屏幕上画画.谁能告诉我如何摆脱菜单?

我附上了一个展示问题的示例类 - 在我的系统上,菜单仍然可见,我希望看到一个完全空白的屏幕.

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;

public class FullScreenFrame extends JFrame implements KeyListener {

    public FullScreenFrame () {
        addKeyListener(this);
        setUndecorated(true);
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

        if (gd.isFullScreenSupported()) {
            try {
                gd.setFullScreenWindow(this);
            }
            finally {
                gd.setFullScreenWindow(null);
            }
        }
        else {
            System.err.println("Full screen not supported");
        }

        setVisible(true);
    }

    public void keyTyped(KeyEvent e) {}
    public void keyPressed(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {
        setVisible(false);
        dispose();
    }

    public static void main (String [] args) {
        new FullScreenFrame();
    }
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*ers 12

我认为你的问题在这里:

try {
        gd.setFullScreenWindow(this);
}
finally {
        gd.setFullScreenWindow(null);
}
Run Code Online (Sandbox Code Playgroud)

finally 块总是被执行,所以这里发生的是你的窗口在短暂的瞬间变成全屏(如果那样)然后立即放弃屏幕.

根据Javadocs的说法,setVisible(true)当你之前打电话时,没有必要.setFullScreenWindow(this)

所以我会将构造函数更改为:

public FullScreenFrame() {
    addKeyListener(this);

    GraphicsDevice gd =
            GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

    if (gd.isFullScreenSupported()) {
        setUndecorated(true);
        gd.setFullScreenWindow(this);
    } else {
        System.err.println("Full screen not supported");
        setSize(100, 100); // just something to let you see the window
        setVisible(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 注意那里的"......"?这是阻止窗口关闭的东西应该去的地方.try ... finally方法只是防范异常,这些异常可能会阻止你的应用程序在完成时释放屏幕.(奇怪的是,即使`setFullScreenWindow`使窗口可见,它也不像`setVisible`那样阻塞.我想知道这是不是设计的.) (2认同)

g.r*_*ket 8

在OS X(10.7及更高版本)上,最好使用可用的本机全屏模式.你应该使用:

com.apple.eawt.FullScreenUtilities.setWindowCanFullScreen(window,true);
com.apple.eawt.Application.getApplication().requestToggleFullScreen(window);
Run Code Online (Sandbox Code Playgroud)

您要全屏显示window的窗口(JFrame等)在哪里