我有这个类,当我像这样添加到这个类时,我创建了一个KeyboardEvent
扩展类,然后按下它(现在)执行的键,但没有打印出来,它没有做任何事情(没有错误) .KeyAdapter
addKeyListener
System.out.println("Here");
public class Ship extends GameObject{ // GameObject Extends JPanel
public Ship(Sprite sprite, ImageObserver o){
super(sprite, o);
this.addKeyListener(new KeyboardEvent());
}
}
但是当我这样做时,它会起作用,也就是打印出“这里”:
public class Game extends JFrame{
public void start(int width, int height){
this.addKeyListener(new KeyboardEvent());
}
}
在其他一些课程中,我运行这个:
package test;
import JGame.Game.Game;
import JGame.Room.Room;
import javax.swing.JFrame;
import test.Rooms.Room1;
public class Main extends JFrame{
public static void main(String[] args){
Game game = new Game("Test Game");
game.start(800, 600);
Room room1 = new Room1(); // This class initiates Ship() it extends Room Which extends JPanel
game.setRoom(room1);
}
}