我在互联网上看到了许多相互矛盾的建议,包括如何使用awt和swing来处理输入,有几个人已经处理了我的代码和它的混乱。
选项
所以我可以
public class Board extends JPanel implements KeyListener{
public Board(){addKeyListener(this);}}
或
public Board(){addKeyListener( new KeyListener(){...});}
或
public class Board extends JPanel {
private class PrivateListener implements KeyListener{...}
public Board(){addKeyListener(new PrivateListener());
或
public class PublicListener implements KeyAdapter{...}
public class Board extends JPanel {
public Board(){addKeyListener(new PublicListener());
或
addKeyListener(this);
addKeyListener(obj1);
addKeyListener(obj2);
和implements KeyListener
可以替换为extends KeyAdapter
,但我不会这样做,因为java只允许一个父类。
然后我不知道这是如何进入我的代码的
private boolean [] keys = new boolean[256];
public void keyPressed(KeyEvent e) {keys[e.getKeyCode()] = true;}
public void keyReleased(KeyEvent e) {keys[e.getKeyCode()] = false;}
public boolean isKeyDown(int k) {return keys[k];}
或
public void keyPressed(KeyEvent e) {
obj1.keyPressed(e);
obj2.keyPressed(e);
}
真的,awt键盘输入的最佳实现是什么?