JAVA代码排错:为何按键处理没有响应?

proof1 发布于 2014/02/05 11:43
阅读 375
收藏 0
import java.applet.*;
import javax.swing.*;
import java.awt.*; import java.awt.event.*;

public class Test extends JFrame {
Game game;

public Test() {
super();
setSize(300, 300);

Container container = getContentPane();
game=new Game();
container.add(game);

setLocationRelativeTo(null);
setVisible(true);
}

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

}

// Game 类
class Game extends JPanel implements Runnable , KeyListener {

Thread thread;

public Game() {
addKeyListener(this);
}

public void play() {
thread = new Thread(this);
thread.start();
}

public void run() {
}

public void keyPressed(KeyEvent e) {
System.out.println("按键处理!!!!");
System.out.println(e.getKeyCode());
}

public void keyTyped(KeyEvent e) { }
public void keyReleased(KeyEvent e) { }

}


为何程序运行后,按键没有反应??JAVA代码排错:为何按键处理没有响应?

加载中
0
NealFeng
NealFeng
public Game() {
addKeyListener(this);
setBorder(BorderFactory.createLineBorder(Color.RED));
}

加个边框看看组件的大小,可能默认布局下组件太小,你点不到


0
返回顶部
顶部