Mac OSX中真正的全屏JFrame/Swing应用程序

iic*_*ain 1 java macos swing fullscreen jframe

我正在开发应用程序,我正在使用Swing制作GUI.我希望我的应用程序是全屏的.我可以很容易地设置窗口的大小,但是我无法使应用程序真正全屏(IE与Apple菜单栏和底座隐藏).我在网上找到的所有答案似乎都不适合我.我是Java的新手,所以感谢任何帮助.

frame = new JFrame("Test");
    frame.setTitle("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel emptyLabel = new JLabel("");
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
    frame.setSize((int)dimension.getWidth(), (int)dimension.getHeight());
    int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2); // X center
    int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);  //Y center
    frame.setLocation(x, y); //Set Frame Location
    frame.setResizable(false); //Frame is not resizable
    frame.setUndecorated(true);  //No decoration for the frame
    frame.setAlwaysOnTop(true);
    frame.setVisible(true); //Make visible
Run Code Online (Sandbox Code Playgroud)

Mad*_*mer 6

Windows和MacOS下的全屏支持有不同的用户期望......

您可以在两者上使用全屏独占模式,但Mac用户在全屏应用程序中有不同的例外,因为MacOS支持OS级别的全屏应用程序

我在使用Java 8的Mavericks 上测试了以下代码(基于这个例子),它运行正常.

public static void enableOSXFullscreen(Window window) {
    try {
        Class util = Class.forName("com.apple.eawt.FullScreenUtilities");
        Class params[] = new Class[]{Window.class, Boolean.TYPE};
        Method method = util.getMethod("setWindowCanFullScreen", params);
        method.invoke(util, window, true);
    } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException ex) {
        ex.printStackTrace();
    }
}

public static void requestOSXFullscreen(Window window) {
    try {
        Class appClass = Class.forName("com.apple.eawt.Application");
        Class params[] = new Class[]{};

        Method getApplication = appClass.getMethod("getApplication", params);
        Object application = getApplication.invoke(appClass);
        Method requestToggleFulLScreen = application.getClass().getMethod("requestToggleFullScreen", Window.class);

        requestToggleFulLScreen.invoke(application, window);
    } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
        ex.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

用户接受您的应用程序时遇到的最困难之一就是满足他们当前的期望.做一些他们不习惯的事情,无论你的应用程序多么精彩,他们都不会喜欢你(恕我直言).