KeyPressed无法正常工作

时间:2015-05-18 23:44:58

标签: java keyevent

 class Card31 extends JPanel implements KeyListener, MouseListener {
     public Card31() {
         addKeyListener(this);
         addMouseListener(this);
         x1 = 700;

     }

     public void keyPressed(KeyEvent e) {

     }

     public void keyReleased(KeyEvent e)

     {

     }

     public void keyTyped(KeyEvent e)

     {

         key = e.getKeyCode();
         if (key == KeyEvent.VK_RIGHT)
             x1 += 10;
         else if (key == KeyEvent.VK_LEFT)
             x1 -= 10;
         repaint();
     }
     public void mouseEntered(MouseEvent e) {
         requestFocus();
     }

     public void mouseExited(MouseEvent e) {}
     public void mousePressed(MouseEvent e) {}
     public void mouseReleased(MouseEvent e) {}

     public void mouseClicked(MouseEvent e) {

     }
     public void mouseClicked(MouseEvent e) {

     }
     public void paintComponent(Graphics g) {
         super.paintComponent(g);
         court2 = Toolkit.getDefaultToolkit().getImage("court2.jpg");
         bhoop = Toolkit.getDefaultToolkit().getImage("bhoop2.png");
         balla = Toolkit.getDefaultToolkit().getImage("balla.png");
         ballc = Toolkit.getDefaultToolkit().getImage("ballc.png");
         balld = Toolkit.getDefaultToolkit().getImage("balld.png");
         balle = Toolkit.getDefaultToolkit().getImage("balle.png");
         ballf = Toolkit.getDefaultToolkit().getImage("ballf.png");
         balli = Toolkit.getDefaultToolkit().getImage("balli.png");
         balll = Toolkit.getDefaultToolkit().getImage("balll.png");
         balln = Toolkit.getDefaultToolkit().getImage("balln.png");
         ballo = Toolkit.getDefaultToolkit().getImage("ballo.png");
         ballr = Toolkit.getDefaultToolkit().getImage("ballr.png");
         balls = Toolkit.getDefaultToolkit().getImage("balls.png");
         ballt = Toolkit.getDefaultToolkit().getImage("ballt.png");
         ballu = Toolkit.getDefaultToolkit().getImage("ballu.png");
         bally = Toolkit.getDefaultToolkit().getImage("bally.png");
         g.drawImage(court2, 0, 0, 1600, 1000, this);
         g.drawImage(ballo, 130, 100, 110, 110, this);
         g.drawImage(ballc, 280, 100, 110, 110, this);
         g.drawImage(balln, 430, 100, 110, 110, this);
         g.drawImage(balld, 580, 100, 110, 110, this);
         g.drawImage(balle, 730, 100, 110, 110, this);
         g.drawImage(ballc, 880, 100, 110, 110, this);
         g.drawImage(balle, 1030, 100, 110, 110, this);
         g.drawImage(ballu, 1180, 100, 110, 110, this);
         g.drawImage(bally, 1330, 100, 110, 110, this);
         g.drawRect(30, 30, 80, 20);
         Font plainfont = new Font("Serif", Font.BOLD, 20);
         g.setFont(plainfont);
         g.setColor(Color.BLACK);
         g.drawString("Score: " + score1, 30, 30);
         g.fillRect(700, 800, 200, 20);
         g.drawImage(bhoop, x1, 500, 150, 150, this);

         g.drawString(msg1, 200, 200);
         System.out.println("x1" + x1);
     }
 }

上面我附上了部分代码,一个嵌套类。 X1是一个全局变量,在构造函数中初始化。 KeyPressed,用于移动Iamge bhoop,不起作用。我已经检查了所有代码以使其正确,但无论如何,Image都不会移动。请帮我解决这个问题。我非常感谢你的帮助< 3。

2 个答案:

答案 0 :(得分:2)

注册KeyListener的组件必须focus才能触发KeyListener。在组件上调用requestFocus以请求组件具有焦点,或者更好地使用KeyBindings

答案 1 :(得分:0)

如此处所述:addKeyListener() doesn't work for JPanel

您的小组需要具有针对性:

this.setFocusable(true);
this.requestFocusInWindow();