2013-08-05 82 views
0

我想使用全屏模式,但當我按下「F」鍵時,我總是收到一個複雜的錯誤。我使用eclipse,它不顯示任何錯誤,並且所有內容都被導入。如果有人擅長代碼,你能告訴我我做錯了什麼嗎?全屏獨佔模式錯誤

的代碼片段:

GraphicsEnviroment ge = GraphicsEnviroment.getLocalGraphicsEnviroment(); 
GraphicsDevice gd = ge.GetDefaultScreenDevice(); 
private boolean FullScreen = false; 
JFrame frame = new JFrame(TITLE); 

// Not shown: The JFrame is set up etc., and a separate class extends KeyAdapter and runs keyPressed 

public void keyPressed(KeyEvent e){ 
    int key = e.getKeyCode(); 
     if(key == KeyEvent.VK_F){ 
     if(!FullScreen){ 
      if(gd.isFullScreenSupported()){ 
       frame.setUndecorated(true); 
       gd.setFullScreenWindow(frame); 
            FullScreen = true; 
      } 
     } else{ 
      frame.setUndecorated(false); 
      gd.setFullScreenWindow(null); 
     } 
    } 

錯誤:

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is displayable. 
at java.awt.Frame.setUndecorated(Frame.java:923) 
at com.Ward.src.main.Game.keyPressed(Game.java:167) 
at com.Ward.src.main.KeyboardInput.keyPressed(KeyboardInput.java:15) 
at java.awt.Component.processKeyEvent(Component.java:6463) 
at java.awt.Component.processEvent(Component.java:6282) 
at java.awt.Component.dispatchEventImpl(Component.java:4861) 
at java.awt.Component.dispatchEvent(Component.java:4687) 
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895) 
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762) 
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027) 
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899) 
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727) 
at java.awt.Component.dispatchEventImpl(Component.java:4731) 
at java.awt.Component.dispatchEvent(Component.java:4687) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) 
at java.awt.EventQueue.access$200(EventQueue.java:103) 
at java.awt.EventQueue$3.run(EventQueue.java:694) 
at java.awt.EventQueue$3.run(EventQueue.java:692) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) 
at java.awt.EventQueue$4.run(EventQueue.java:708) 
at java.awt.EventQueue$4.run(EventQueue.java:706) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 

回答

0

在使用之前,您需要初始化gdGraphicsEnvironment.getGraphicsEnvironment.getDefaultScreenDevice()

更新:既然OP已經解決了這個問題,還有另一個問題。問題是在已經顯示的窗口上更改​​屬性。如果正在顯示窗口,則無法更改​​屬性,因爲操作系統在顯示時無法更改屬性。我認爲這可以通過省略對setUndecorated的調用來解決,全屏窗口不必非修飾(我認爲)。

+0

我換成了GraphicsDevice gd;與GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); \t GraphicsDevice gd = ge.getDefaultScreenDevice();但我仍然得到錯誤!幫幫我! – user2653616

+0

更正錯誤後修復當前錯誤 – user2653616

+0

我更新了我的答案 – tbodt

0

是這樣的:

if(gd.isFullScreenSupported()){ 

線165?如果是這樣,gd set是什麼?

上述例外給出了發生錯誤的類/行號。這應該足以讓你知道什麼是空的。

+0

我認爲frame.setUndecorated(真);現在正在返回一個錯誤。可以返回一個錯誤? – user2653616

1
frame.dispose(); // Fixes the issue 
frame.setUndecorated(true); 
frame.setVisible(); 

我知道這個問題是舊的,但我只是處理這個問題我自己,解決工作正常,我。

0

在您的代碼中只添加「setUndecorated(true)」。 實施例:

public launch() { 

    setSize(300, 200); 
    setUndecorated(true); 

}