2013-10-28 75 views
2

在我的節目,我有沒有辦法正常的窗口和全屏狀態之間切換:的KeyListener不配置後工作()

public void setFullScreen(boolean enableFullScreen) { 
    Frame.fullScreen = enableFullScreen; 
    dispose(); 
    if (enableFullScreen) { 
     getContentPane().setPreferredSize(resFull); 
     setSize(resFull); 
     setBounds(Frame.graphicsDev.getConfigurations()[0].getBounds().x, Frame.graphicsDev.getConfigurations()[0].getBounds().y, resFull.width, resFull.height); 
     //code for resizing components 
     setUndecorated(true); 
     setResizable(false); 
    } else { 
     getContentPane().setSize(resNormal); 
     getContentPane().setPreferredSize(resNormal); 
     //code for resizing components 
     setUndecorated(false); 
     setResizable(true); 
    } 
    pack(); 
    setVisible(true); 
} 

resFullresNormal是維變量。

當我點擊打開正常的窗口F11它改變全屏狀態(setFullScreen(!Frame.fullScreen)),但改變後通常不會收到任何信號 - 我要切換到其它應用程序,然後轉回到這個窗口 - 它的工作原理。

+2

你在部署和處置爲什麼呢? Dispose是用於刪除您調用它的任何內容。 – Radiodef

+1

@Radiodef:實際配置不是爲了「刪除」,而是爲了釋放本地資源。在顯示它之前,你絕對不想放置頂層窗口,但調用'pack()'和'setVisible(true)'應該重新構成本地資源。 –

+0

我不得不這樣做,因爲我想調用'setUndecorated()'。 – MarPiRK

回答

0

可能你的問題是由於JButton被按下時竊取焦點導致你的KeyListener正在監聽未聚焦的組件。再一個解決方案是使用鍵綁定。

sscce,以證明自己既KeyListeners和鍵綁定,你試圖實現轉型後的正常工作:

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.event.*; 

import javax.swing.*; 

@SuppressWarnings("serial") 
public class TestUndecoratedKeyListener { 
    protected static final int PREF_W = 400; 
    protected static final int PREF_H = PREF_W; 

    private static void createAndShowGui() { 
     final JFrame frame = new JFrame("TestUndecoratedKeyListener"); 

     final JPanel mainPanel = new JPanel() { 
     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(PREF_W, PREF_H); 
     } 
     }; 

     JToggleButton toggleButton = new JToggleButton("Full Screen"); 
     toggleButton.setFocusable(false); 
     toggleButton.setMnemonic(KeyEvent.VK_F); 
     toggleButton.addItemListener(new ItemListener() { 

     @Override 
     public void itemStateChanged(ItemEvent e) { 
      if (e.getStateChange() == ItemEvent.SELECTED) { 
       frame.dispose(); 
       frame.setUndecorated(true); 
       frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
       frame.setVisible(true); 
      } else { 
       frame.dispose(); 
       frame.setUndecorated(false); 
       frame.setExtendedState(JFrame.NORMAL); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     } 
     }); 
     mainPanel.add(toggleButton); 
     final JLabel keyListenerLabel = new JLabel(); 
     final JLabel keyBindingLabel = new JLabel(); 

     mainPanel.add(new JLabel("Press Right or Left Arrow Key To Test")); 
     mainPanel.add(keyListenerLabel); 
     mainPanel.add(keyBindingLabel); 

     keyListenerLabel.setBorder(BorderFactory.createLineBorder(Color.black)); 
     keyBindingLabel.setBorder(BorderFactory.createLineBorder(Color.black)); 

     mainPanel.addKeyListener(new KeyAdapter() { 

     @Override 
     public void keyPressed(KeyEvent e) { 
      if (e.getKeyCode() == KeyEvent.VK_LEFT) { 
       keyListenerLabel.setText("KeyListener: left pressed"); 
      } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { 
       keyListenerLabel.setText("KeyListener: right pressed"); 
      } 
     } 
     }); 

     int condition = JPanel.WHEN_IN_FOCUSED_WINDOW; 
     InputMap inputMap = mainPanel.getInputMap(condition); 
     ActionMap actionMap = mainPanel.getActionMap(); 

     KeyStroke leftArrowStroke = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0); 
     KeyStroke rightArrowStroke = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0); 
     inputMap.put(leftArrowStroke, leftArrowStroke.toString()); 
     inputMap.put(rightArrowStroke, rightArrowStroke.toString()); 
     actionMap.put(leftArrowStroke.toString(), new AbstractAction() { 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      keyBindingLabel.setText("Key Bindings: left Arrow Pressed"); 
     } 
     }); 
     actionMap.put(rightArrowStroke.toString(), new AbstractAction() { 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      keyBindingLabel.setText("Key Bindings: right Arrow Pressed"); 
     } 
     }); 


     mainPanel.setFocusable(true); 
     mainPanel.requestFocusInWindow(); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 
+0

'mainPanel.requestFocusInWindow();'即使對於KeyListener也行。謝謝。 – MarPiRK