2011-12-12 162 views
7

我已經使用Java編寫了KeyListener示例代碼, 我創建了JPanel,然後將其焦點設置爲true,我創建了KeyListener,請求焦點並將KeyListener添加到我的面板。但是keyListener的方法永遠不會被調用。看起來,儘管我已經請求了重點,但它並不關注。java keylistener not called

任何人都可以幫忙嗎?

listener = new KeyLis(); 
this.setFocusable(true); 
this.requestFocus(); 
this.addKeyListener(listener); 

class KeyLis implements KeyListener{ 

    @Override 
    public void keyPressed(KeyEvent e) { 
     currentver += 5; 
     switch (e.getKeyCode()) { 
      case KeyEvent.VK_LEFT : if(horizontalyInBounds()) currentPos-= 5; 
       break; 
      case KeyEvent.VK_RIGHT: if(horizontalyInBounds()) currentPos+= 5; 
       break; 
     } 
     repaint(); 
    } 

    @Override 
    public void keyReleased(KeyEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void keyTyped(KeyEvent e) { 
    } 
} 

如果要進行一些必要的運行的代碼:

import java.awt.Color; 
    import java.awt.Graphics; 
    import java.util.Random; 

    import javax.swing.JFrame; 
    import javax.swing.JLabel; 


public class test extends JFrame { 

private AreaOfGame areaOfGame; 

public test() 
{ 
    super(""); 
    setVisible(true); 
    this.setBackground(Color.darkGray); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.pack(); 
    setLayout(null); 
    setBounds(200, 10, 400, 700); 

    areaOfGame = new AreaOfGame(); 
    this.add(areaOfGame); 

    startGame(); 
} 

public int generateNext() 
{ 
    Random r = new Random(); 
    int n = r.nextInt(7); 
    return n; 
} 

public void startGame() 
{ 
    while(!areaOfGame.GameOver()) 
    { 
     areaOfGame.startGame(generateNext()); 
    } 
} 


public static void main(String[] args) { 
    new MainFrame(); 
} 


import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 

import javax.swing.JPanel; 

public class AreaOfGame extends JPanel { 


    private static final int rightside = 370; 

    private int bottom; 
    private int top; 

    private int currentPos; 
    private int currentver; 
    private KeyLis listener; 

    public AreaOfGame() 
    { 
     super(); 

     bottom = 650; 
     top = 50; 
     setLayout(null); 
     setBounds(20, 50, 350, 600); 
     setVisible(true); 


     this.setBackground(Color.lightGray); 

     listener = new KeyLis(); 
     this.setFocusable(true); 
     if(this.requestFocus(true)) 
      System.out.println("true");; 
     this.addKeyListener(listener); 


     currentPos = 150; 
     currentver=0; 
    } 

    public void startGame(int n) 
    { 
     while(verticallyInBound()){ 
      System.out.println("anything"); 

     } 


    } 

    public boolean verticallyInBound() 
    { 
     if(currentPos<= bottom -50) 
      return true; 
     return false; 
    } 


    public boolean GameOver() 
    { 
     if(top>= bottom){ 
      System.out.println("game over"); 
      return true; 
     } 

     else return false; 
    } 


    public boolean horizontalyInBounds() 
    { 
     if(currentPos<=rightside && currentPos>= 20) 
      return true; 
     else return false; 
    } 


class KeyLis implements KeyListener{ 

     @Override 
     public void keyPressed(KeyEvent e) { 
      System.out.println("called"); 
      currentver += 5; 
      switch (e.getKeyCode()) { 
       case KeyEvent.VK_LEFT : if(horizontalyInBounds()) currentPos-= 5; break; 
       case KeyEvent.VK_RIGHT: if(horizontalyInBounds()) currentPos+= 5; break; 
      } 
      repaint(); 


     } 

     @Override 
     public void keyReleased(KeyEvent e) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void keyTyped(KeyEvent e) { 
      System.out.println("called 3"); 
     } 
} 

} 
+1

這將有助於如果你能告訴我們您的「KeyLis」類,也許問題的存在。 – eboix

+0

謝謝,我添加了聽衆類 – BBB

+0

嗯......我想我曾經有過類似的問題。嘗試將方法「e.consume()」調用到keyPressed(KeyEvent e)方法的末尾。讓我知道它是否有效。 – eboix

回答

12

我敢打賭,你要請求焦點的JPanel已經呈現前(頂層窗口或者收到pack()setVisible(true)稱爲),如果是這樣,這將無法正常工作。僅在組件已被渲染後,纔可能授予焦點請求。你是否檢查過你撥打requestFocus()的電話?它必須爲你的來電返回真正的成功機會。此外,最好使用requestFocusInWindow()而不是requestFocus()

但更重要的是,您不應該爲此使用KeyListeners,而應該使用鍵綁定,這是Swing本身用於響應按鍵的更高級別的概念。

編輯
SSCCE一個例子:

import java.awt.Dimension; 
import java.awt.event.*; 
import javax.swing.*; 

public class TestKeyListener extends JPanel { 
    private KeyLis listener; 

    public TestKeyListener() { 
     add(new JButton("Foo")); // something to draw off focus 
     listener = new KeyLis(); 
     this.setFocusable(true); 
     this.requestFocus(); 
     this.addKeyListener(listener); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(300, 200); 
    } 

    private class KeyLis extends KeyAdapter { 
     @Override 
     public void keyPressed(KeyEvent e) { 
     switch (e.getKeyCode()) { 
     case KeyEvent.VK_LEFT: 
      System.out.println("VK_LEFT pressed"); 
      break; 
     case KeyEvent.VK_RIGHT: 
      System.out.println("VK_RIGHT pressed"); 
      break; 
     } 
     } 
    } 

    private static void createAndShowGui() { 
     TestKeyListener mainPanel = new TestKeyListener(); 

     JFrame frame = new JFrame("TestKeyListener"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

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

編輯2
並採用鍵綁定等效SSCCE

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class TestKeyBindings extends JPanel { 

    public TestKeyBindings() { 
     add(new JButton("Foo")); // something to draw off focus 
     setKeyBindings(); 
    } 

    private void setKeyBindings() { 
     ActionMap actionMap = getActionMap(); 
     int condition = JComponent.WHEN_IN_FOCUSED_WINDOW; 
     InputMap inputMap = getInputMap(condition); 

     String vkLeft = "VK_LEFT"; 
     String vkRight = "VK_RIGHT"; 
     inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), vkLeft); 
     inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), vkRight); 

     actionMap.put(vkLeft, new KeyAction(vkLeft)); 
     actionMap.put(vkRight, new KeyAction(vkRight)); 

    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(300, 200); 
    } 

    private class KeyAction extends AbstractAction { 
     public KeyAction(String actionCommand) { 
     putValue(ACTION_COMMAND_KEY, actionCommand); 
     } 

     @Override 
     public void actionPerformed(ActionEvent actionEvt) { 
     System.out.println(actionEvt.getActionCommand() + " pressed"); 
     } 
    } 

    private static void createAndShowGui() { 
     TestKeyBindings mainPanel = new TestKeyBindings(); 

     JFrame frame = new JFrame("TestKeyListener"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

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

編輯3
關於您最近的SSCCE,您的while (true)循環阻塞了您的Swing事件線程,並可能阻止用戶交互或繪畫發生。最好使用Swing Timer而不是while (true)。例如:

import java.awt.*; 
import java.awt.event.*; 
import java.util.Random; 

import javax.swing.*; 

public class BbbTest extends JFrame { 

    private AreaOfGame areaOfGame; 

    public BbbTest() { 
     super(""); 
//  setVisible(true); 
     this.setBackground(Color.darkGray); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.pack(); 
     setLayout(null); 
     setBounds(200, 10, 400, 700); 

     areaOfGame = new AreaOfGame(); 
     this.add(areaOfGame); 
     setVisible(true); 

     startGame(); 
    } 

    public int generateNext() { 
     Random r = new Random(); 
     int n = r.nextInt(7); 
     return n; 
    } 

    public void startGame() { 
     // while (!areaOfGame.GameOver()) { 
     // areaOfGame.startGame(generateNext()); 
     // } 

     areaOfGame.startGame(generateNext()); 
    } 

    public static void main(String[] args) { 
     new BbbTest(); 
    } 

    class AreaOfGame extends JPanel { 

     private static final int rightside = 370; 

     private int bottom; 
     private int top; 

     private int currentPos; 
     private int currentver; 
     private KeyLis listener; 

     public AreaOfGame() { 
     super(); 

     bottom = 650; 
     top = 50; 
     setLayout(null); 
     setBounds(20, 50, 350, 600); 
     setVisible(true); 

     this.setBackground(Color.lightGray); 

     listener = new KeyLis(); 
     this.setFocusable(true); 
     if (this.requestFocus(true)) 
      System.out.println("true"); 
     ; 
     this.addKeyListener(listener); 

     currentPos = 150; 
     currentver = 0; 
     } 

     public void startGame(int n) { 
     // while (verticallyInBound()) { 
     // System.out.println("anything"); 
     // } 

     int timeDelay = 50; // msecs delay 
     new Timer(timeDelay , new ActionListener() { 

      public void actionPerformed(ActionEvent arg0) { 
       System.out.println("anything"); 
      } 
     }).start(); 

     } 

     public boolean verticallyInBound() { 
     if (currentPos <= bottom - 50) 
      return true; 
     return false; 
     } 

     public boolean GameOver() { 
     if (top >= bottom) { 
      System.out.println("game over"); 
      return true; 
     } 

     else 
      return false; 
     } 

     public boolean horizontalyInBounds() { 
     if (currentPos <= rightside && currentPos >= 20) 
      return true; 
     else 
      return false; 
     } 

     class KeyLis implements KeyListener { 

     @Override 
     public void keyPressed(KeyEvent e) { 
      System.out.println("called"); 
      currentver += 5; 
      switch (e.getKeyCode()) { 
      case KeyEvent.VK_LEFT: 
       if (horizontalyInBounds()) 
        currentPos -= 5; 
       break; 
      case KeyEvent.VK_RIGHT: 
       if (horizontalyInBounds()) 
        currentPos += 5; 
       break; 
      } 
      repaint(); 

     } 

     @Override 
     public void keyReleased(KeyEvent e) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void keyTyped(KeyEvent e) { 
      System.out.println("called 3"); 
     } 
     } 
    } 
} 
+0

其實這些都是在我的框架有pack()和setVisible(true)之後完成的。如果使用鍵綁定是我唯一的選擇,但我真的很想知道爲什麼這不起作用,因爲沒有看起來像身體在之前有這個問題,我檢查了空指針,焦點,被調用的方法,偵聽器被添加,但沒有工作。 – BBB

+0

@BBB:再次,你檢查了你的'requestFocus()'方法調用已經返回嗎?它返回真或假? –

+0

它始終返回false – BBB

0

可以使用「TAB」按鈕在按鈕和按鍵監聽器之間切換。 我有一個按鈕的程序,按下按鈕後,按鍵偵聽器不起作用。 我意識到,如果按下「TAB」按鈕,程序的「注意」或「焦點」將返回到按鍵監聽器。

也許這將幫助:http://docstore.mik.ua/orelly/java-ent/jfc/ch03_08.htm