Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 views
in Technique[技术] by (71.8m points)

java - True Full Screen JFrame/Swing Application in Mac OSX

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...