我正在尝试实现一项功能,其中当我按空格键时会切换计时器。不幸的是,空格键没有反应,我不确定自己做错了什么。任何帮助都非常感谢!
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class Life extends JPanel implements ActionListener {
public static final int SIZE = 15; // map dimension
public static final int CELL_SIZE = 25; //size of cells
public static final int UPDATE = 1000; //rate of updating
public char[][] map; // current values of map
public char[][]temp_map; // store changes while calculating
//constructor
public Life() {
map = new char[SIZE][SIZE];
temp_map = new char[SIZE][SIZE];
//to hear key events
KeyResponder key = new KeyResponder();
addKeyListener(new KeyResponder());
setFocusable(true);
}
// Inner class for responding to key events
private class KeyResponder extends KeyAdapter {
// Method that runs whenever a key is pressed
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_SPACE:
System.out.println("test");
pause();
break;
}
}
}