我在使用KeyListener时遇到问题。我不断收到错误消息,说控制器不是抽象的,并且没有覆盖java.awt.event.KeyListener中的抽象方法keyReleased(java.awt.event.KeyEvent),但是我已经实现了keyReleased。还有其他修复建议吗?下面是我的代码:
public class Controller extends JPanel implements KeyListener, Runnable{
//EventHandler<KeyEvent>{
@FXML private Label scoreLabel;
@FXML private Label messageLabel;
@FXML private GameView gameView;
private Thread thread;
private boolean running = false;
private Scene scene;
Timer timer;
private Model model;
public Controller() {
}
public void keyPressed(KeyEvent e) {
KeyCode code = e.getCode();
if (code == KeyCode.LEFT || code == KeyCode.A) {
this.model.setDirection("left");
} else if (code == KeyCode.RIGHT || code == KeyCode.D) {
this.model.setDirection("right");
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void initialize() {
this.model = new Model(this.gameView.getRowCount(),this.gameView.getColumnCount());
timer = new Timer();
this.update();
}
private void update() {
if (this.model.getDirection().equals("right")){this.model.moveMoodler("right");}
else if (this.model.getDirection().equals("left"){this.model.moveMoodler("left");}
this.gameView.update(this.model);
}
public double getBoardWidth() {
return GameView.CELL_WIDTH * this.gameView.getColumnCount();
}
public double getBoardHeight() {
return GameView.CELL_WIDTH * this.gameView.getRowCount();
}
public void start(Scene scene) {
thread = new Thread(this);
this.scene = scene;
thread.start();
running = true;
}
public void stop() {
try {
thread.join();
running = false;
}catch(Exception e) {
e.printStackTrace();
}
}
public void run() {
TimerTask task = new TimerTask()
{
public void run()
{
//System.out.println("In timer task");
model.move();
update();
}
};
while (true) {
timer.schedule(task, 100);
}
}
public void tick() {
}
public void render() {
}
/**
* Pops up start page
* Transitions into playGame mode after
* start button is pressed
*/
public void startGame(){
}
/**
* Moves from playing game page to end page
* Updates high score if need be
* Offers replay button
*/
public void endGame() {
}
/**
* Takes user input to play the actual game
*/
public void playGame(){
}
}
谢谢!