因此,我从tank1类创建了两个对象,它们的名称分别为tank1和tank2。我希望tank1用RIGHT键向右移动,并用LEFT键向左移动,而tank2用D键向右移动,而A键向左移动。但是,当我完成代码编译后,tank2用A和LEFT键向左移动,而D和RIGHT键则向右移动,tank1根本不移动任何键。有什么办法可以解决这个问题,让坦克1向左,向右键移动?
这是我的gridbasegame类:
public class GridBasedGameDriver {
private JFrame frame = new JFrame("my world");
private JPanel panel;
private List<Drawable> drawables= new ArrayList();
private Terrain terrain;
private Tank1 Tank1;
private Tank1 Tank2;
public static void main(String[] args) {
new GridBasedGameDriver().start();
}
private void start() { // REPAINT
setUpGame();
frame.setBackground(new Color(127, 127, 127));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel() { public void paintComponent(Graphics g) { super.paintComponent(g); drawGame(g); } }; // what does paint componet and draw game do,
//and where does the
//super come from?
panel.setPreferredSize(new Dimension(800,600)); // doesn't matter, can be set again manually
frame.add(panel); // so frame needs to add panel
frame.pack(); // no idea, probably not important
// int b= (int)(Math.random()*100+100); //100-199, only here for fun doesn't change a damn thing
panel.getInputMap().put(KeyStroke.getKeyStroke("RIGHT"),"slideRight");
panel.getActionMap().put("slideRight",new AbstractAction(){
public void actionPerformed(ActionEvent arg0) {
Tank1.moveRight();
panel.repaint();
}
});
panel.requestFocusInWindow();
panel.getInputMap().put(KeyStroke.getKeyStroke("LEFT"),"slideLeft");
panel.getActionMap().put("slideLeft",new AbstractAction(){
public void actionPerformed(ActionEvent arg0) {
Tank1.moveleft();
panel.repaint();
}
});
panel.requestFocusInWindow();
panel.getInputMap().put(KeyStroke.getKeyStroke("D"),"slideRight");
panel.getActionMap().put("slideRight",new AbstractAction(){
public void actionPerformed(ActionEvent arg0) {
Tank2.moveRight();
panel.repaint();
}
});
panel.requestFocusInWindow();
panel.getInputMap().put(KeyStroke.getKeyStroke("A"),"slideLeft");
panel.getActionMap().put("slideLeft",new AbstractAction(){
public void actionPerformed(ActionEvent arg0) {
Tank2.moveleft();
panel.repaint();
}
});
panel.requestFocusInWindow();
setUpObjects();
frame.repaint();
}
private void setUpObjects() {
terrain = new Terrain(panel.getWidth(), panel.getHeight()); // where does the panel come from?
terrain.initialize();
List<Integer> b=terrain.getlist();
Tank1 = new Tank1(20,0,b);
Tank2 = new Tank1(740,1,b);
drawables.add(terrain); // I get it, so the terrain has drawable, and once it gets added to drawable array it implements its own drawable
drawables.add(Tank1);
drawables.add(Tank2);
}
public void drawGame(Graphics g) {
for(Drawable dr:drawables) {
dr.draw(g);
}
}
这是我的tank1类:
public class Tank1 implements Drawable {
public int gi;
public int it;
public List<Integer> b;
List<Integer> Krell = new ArrayList<>();
public Tank1(int gi, int it, List<Integer> b) {
this.b=b;
this.gi=gi;
this.it=it;
}
public void moveleft() {
gi=gi-1;
}
public void moveRight() {
gi=gi+1;
}
public void draw(Graphics g) {
// TODO Auto-generated method student
if (it==0) {
g.setColor(new Color(230,50,58)); // draws that recoatlve
}
if(it==1) {
g.setColor(new Color(120,160,60)); // draws that recoatlve
}
g.fillRect(gi, b.get(gi)-25, 25, 25); //draws that rectangle
}
}
最佳答案
可能首先想到的是,默认情况下JPanel
不可聚焦,因此在窗口中请求聚焦将不会执行任何操作。此外,使用这样的键绑定的目的是避免必须处理“抓紧焦点”样式的hack。getInputMap
有几个变体,一个变体允许您定义焦点上下文,在该上下文中将触发绑定。您可以考虑使用WHEN_IN_FOCUSED_WINDOW
,例如...
panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("RIGHT"),"slideRight");