Mac OSX 中真正的全屏 JFrame/Swing 应用程序 [英] True Full Screen JFrame/Swing Application in Mac OSX

查看:25
本文介绍了Mac OSX 中真正的全屏 JFrame/Swing 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发应用程序,并且正在使用 Swing 制作 GUI.我希望我的应用程序是全屏的.我可以轻松设置窗口的大小,但是我无法使应用程序真正全屏(IE 带有苹果菜单栏和停靠栏隐藏).我在网上找到的所有答案似乎都不适合我.我是 Java 新手,因此不胜感激.

I'm working on application and I'm making the GUI with Swing. I would like my application to be fullscreen. I can easily set the size of the window however I have not been able to make the application truly fullscreen (IE With the apple menu bar and dock hidden). All the answers I have found online don't seem to work for me. I'm new to Java so any help is appreciated.

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

推荐答案

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

Full screen support under Windows and MacOS have different user expectations...

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

You could use Full screen exclusive mode on both, but Mac users have a different exceptions when it comes to full screen applications, as MacOS supports full screen applications at an OS level

我使用 Java 8 在 Mavericks 上测试了以下代码(基于 this example)而且效果很好.

I tested the following code (which is based on this example) on Mavericks with Java 8 and it works fine.

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();
    }
}

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

One of the hardest hurdles you will have with users accepting your application is working with their current expectations. Do something they aren't use to and no matter how wonderful your app is, they won't like you for it (IMHO).

这篇关于Mac OSX 中真正的全屏 JFrame/Swing 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆