public class Board extends JPanel implements Runnable, KeyListener{
public static final int HEIGHT = 950;
public static final int WIDTH = 1000;
private JFrame jf;
private Enemy enemy;
private boolean board[][];
public Board( int r, int col) {
this.rows = r;
this.columns = col;
enemy = new Enemy(rows, columns, this, extraEnemy);
board = new boolean[rows + extraEnemy][columns + extraEnemy];
setSize(WIDTH , HEIGHT);
jf = new JFrame();
jf.setSize(WIDTH , HEIGHT);
(new Thread(this)).start();
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
jf.setLocation((d.width - 950) / 2, (d.height - 1000) / 2);
jf.add(this);
jf.addKeyListener(this);
jf.getContentPane().add(this);
jf.getContentPane().add(player);
jf.setLayout(null);
jf.setVisible(true);
}
@Override
public void run() {
while (true) {
long d = System.currentTimeMillis();
while (d + 100 > System.currentTimeMillis());
this.repaint();
}
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println(" hereeee");
if(e.getKeyCode() == VK_SPACE){
// bullet = new Bullet(player.getX(), player.getY());
// bulletVec.addElement(bullet);
// clickedOn =true;
}
}
我正在Java Swing中编写一个程序,其中我有一个JPanel
,在里面,我定义了一个JFrame
,可以帮助我将keyListener
添加到我的面板中。但是,虽然我编写了addKeyListener
方法,但该程序并未输入keyPressed
方法。