日期:2014-05-20  浏览次数:20725 次

麻烦高手帮忙看一下,为什么不能监听键盘事件
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Pete extends JFrame
{
  PetePanel pete =new PetePanel();
  public Pete()
  {
  super("Pixel Pete");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  JPanel pane=new JPanel();
pane.setLayout(new GridLayout(1,1,15,15));
pane.add(pete);
setContentPane(pane);
show();
}
public static void main(String[] arguments)
{
Pete Penguin =new Pete();
PetePanel a=new PetePanel();
}
}
class PetePanel extends JPanel implements KeyListener//,Runnable

  //JPanel.requestFocus(); 
//Thread runner;
Image petePics[]=new Image[4];
Image back;
int current=0;
int x=100;
int y=100;
PetePanel()
{
super();
setBackground(Color.black);
String peteSrc[]={"right.jpg","left.jpg","up.jpg","down.jpg"};
Toolkit kit = Toolkit.getDefaultToolkit();
for(int i=0;i<petePics.length;i++)
{
petePics[i]=kit.getImage(peteSrc[i]);
}
back=kit.getImage("abc.bmp");
//if(runner==null)
//{
//runner=new Thread(this);
//runner.start();
//}

}
public void paint(Graphics comp)
{
Graphics2D comp2D=(Graphics2D)comp;
//if(back!=null)
//comp2D.drawImage(back,0,0,this);
comp2D.setColor(Color.black);
comp2D.fillRect(0,0,500,500);
if(petePics[current]!=null)
comp2D.drawImage(petePics[current],x,y,this);
}
public void fun()
{
for(int i=0;i<500;i++)
{
x=x+5;
current=1;
repaint();
}

this.addKeyListener(new KeyAdapter() {
  @Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
current=1;
x=x-10;
}
else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
current=0;
x=x+10;
}
else if(e.getKeyCode()==KeyEvent.VK_UP)
{
current=2;
y=y-10;
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
current=3;
y=y-10;
}
repaint();
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
});

}}


------解决方案--------------------
Java code

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

public class Pete extends JFrame {
    PetePanel pete = new PetePanel();

    public Pete() {
        super("Pixel Pete");
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel pane = new JPanel();
        pane.setLayout(new GridLayout(1, 1, 15, 15));
        pane.add(pete);
        this.addKeyListener(new KeyAdapter(){//没有得到焦点,放frame容易获取焦点。
            @Override
            public void keyPressed(KeyEvent e) {
                System.out.println("ppppp");
                if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                    pete.current = 1;
                    pete.x =pete.x - 10;
                } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                    pete.current = 0;
                    pete.x = pete.x + 10;
                } else if (e.getKeyCode() == KeyEvent.VK_UP) {
                    pete.current = 2;
                    pete.y = pete.y - 10;
                } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                    pete.current = 3;
                    pete.y = pete.y - 10;
                }
                repaint();
            }

            public void keyTyped(KeyEvent e){
                System.out.println("kkkkk");
            }

            public void keyReleased(KeyEvent e) {
                System.out.println("rrrrrr");
            }
        });
        setContentPane(pane);
        this.setVisible(true);
        this.requestFocus();//具体办法可以手设焦点。如果焦点有问题,就优先想到它
    }

    public static void main(String[] arguments) {
        Pete Penguin = new Pete();
        PetePanel a = new PetePanel();
    }
}

class PetePanel extends JPanel{
    // JPanel.requestFocus();
    // Thread runner;
    Image petePics[] = new Image[4];
    Image back;
    int current = 0;
    int x = 100;
    int y = 100;

    PetePanel() {
        super();
        setBackground(Color.black);
        String peteSrc[] = { "right.jpg", "left.jpg", "up.jpg", "down.jpg" };
        Toolkit kit = Toolkit.getDefaultToolkit();
        for (int i = 0; i < petePics.length; i++) {
            petePics[i] = kit.getImage(peteSrc[i]);
        }
        back = kit.getImage("abc.bmp");
        // if(runner==null)
        // {
        // runner=new Thread(this);
        // runner.start();
        // }

    }

    public void paint(Graphics comp) {
        Graphics2D comp2D = (Graphics2D) comp;
        // if(back!=null)
        // comp2D.drawImage(back,0,0,this);
        comp2D.setColor(Color.black);
        comp2D.fillRect(0, 0, 500, 500);
        if (petePics[current] != null)
            comp2D.drawImage(petePics[current], x, y, this);
    }

    public void fun() {
        for (int i = 0; i < 500; i++) {
            x = x + 5;
            current = 1;
            repaint();
        }
        
    }
}