2011-06-12 54 views
1

我有Java構建一個遊戲在全屏模式下,我用這個代碼去全屏:esacpe鍵退出全屏模式下的Java

this.setPreferredSize(new Dimension((int)screenSize.getWidth(), (int)screenSize.getHeight())); 
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); 
     if(gd.isFullScreenSupported()) { 
      this.setUndecorated(true); 
      gd.setFullScreenWindow(this); 
     } 
     else { 
      System.err.println("Full screen not supported!!"); 

     } 

在某些Windows系統時,我按Esc鍵程序將退出全屏模式並最小化。我如何阻止這個「功能」?

謝謝。

回答

1

我發現了這個問題,當我切換視圖時,程序失去了焦點,所以windows會處理ESCAPE鍵盤事件。每次視圖更改時,我都會通過請求焦點來解決問題。

這樣的:

this.addComponentListener(new ComponentAdapter() { 
      @Override 
      public void componentShown(ComponentEvent e) { 
       View.this.requestFocusInWindow(); 
      }    
     }); 
1

編寫您自己的按鍵偵聽器,並按照您的要求處理按鍵。你可以看看this

編輯:this似乎可以解釋它更好。

1

如果這是一個使用Swing的,那麼你應該使用Key Bindings。 Swing旨在使用KeyBindings,而不是KeyEvents。

+0

使用鍵綁定和keyListeners消耗ESC鍵事件中,我已經嘗試過,但沒有效果。 – Jesse 2011-06-12 20:37:42