我正在制作一个游戏,用户必须按键才能移动。我正在使用键绑定,但它们不起作用。键绑定应该调用 Wp 类并打印“W press”,但没有任何反应。这是代码:
public class SO extends JFrame {
public static void main(String[] args) {
new SO();
}
C c;
public SO(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
c=new C();
c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("W"), "wp");
c.getActionMap().put("wp", new Wp());
this.setVisible(true);
}
private class C extends JComponent {
public void paint(Graphics g){}
}
private class Wp extends AbstractAction {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("W pressed");
}
}
}
相关分类