当我明确地将一些键绑定分配给另一个对象时,为什么我的所有键绑定仅适用于一个对象?

时间:2018-02-21 05:04:53

标签: java swing key-bindings

所以我从tank1类创建了两个对象,它们的名字是tank1和tank2。我希望tank1用RIGHT键向右移动,用左键向左移动,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

    }   
    }

1 个答案:

答案 0 :(得分:1)

可能首先想到的是,JPanel默认情况下不可聚焦,因此请求窗口中的焦点不会做任何事情。此外,使用这样的键绑定的目的是避免必须处理&#34;抓住焦点&#34;风格黑客。

getInputMap有几种变体,其中一种变体允许您定义将触发绑定的焦点上下文。您可以考虑使用WHEN_IN_FOCUSED_WINDOW,例如......

panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("RIGHT"),"slideRight");