如何解决Mac OSX中的真正全屏JFrame / Swing应用程序
Windows和MacOS下的全屏支持对用户的期望不同…
您可以在两者上使用全屏独占模式,但是Mac用户在全屏应用程序方面会有不同的例外,因为MacOS在操作系统级别支持全屏应用程序
我在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();
}
}
用户接受您的应用程序时遇到的最困难的障碍之一就是符合他们当前的期望。做一些他们不习惯的事情,无论您的应用多么出色,他们都不会喜欢您的(IMHO)。
解决方法
我正在开发应用程序,并且正在使用Swing制作GUI。我希望我的应用程序是全屏的。我可以轻松设置窗口的大小,但是我无法使应用程序真正全屏显示(即带有苹果菜单栏和停靠栏的IE)。我在网上找到的所有答案似乎都不适合我。我是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