2012-10-05 74 views

回答

4

看看

UPDATE

幸运的是JFrame延伸以便于通过FrameWindow ...

enter image description here

public class TestMacFullScreen { 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setSize(200, 200); 
       frame.setLocationRelativeTo(null); 
       frame.setLayout(new BorderLayout()); 

       JLabel label = new JLabel("Look ma, no hands"); 

       frame.add(label); 

       enableOSXFullscreen(frame); 

       frame.setVisible(true); 


      } 
     }); 
    } 

    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 (ClassNotFoundException exp) { 
      exp.printStackTrace(); 
     } catch (Exception exp) { 
      exp.printStackTrace(); 
     } 
    } 
} 
+0

这看起来像什么即时寻找(第一个),但有一个有点麻烦IM实现它。我相当新的Java。该功能需要一个窗口,但如何从JFrame获取窗口? – bobjamin

+0

检查更新... – MadProgrammer

+0

谢谢你,我的代码不工作,因为我在调用全屏方法之前有setVisible。显然它需要隐藏窗口才能进行操作。 – bobjamin