Java 无法实现键绑定

Java 无法实现键绑定,java,swing,key-bindings,Java,Swing,Key Bindings,我试图使用KeyBinding移动rectange,但我想我无法正确实现它。 我是java的初学者,在这个程序中找不到错误 所以请帮帮我 提前通知 import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.AbstractAction; import javax.swing.JFrame; import javax.sw

我试图使用KeyBinding移动rectange,但我想我无法正确实现它。 我是java的初学者,在这个程序中找不到错误 所以请帮帮我

提前通知

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

class Move {

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                abc();
            }
        });
    }

    private static void abc() {
        JFrame frame = new JFrame("moving object");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1300, 600);
        frame.add(new P1());
    }
}

class P1 extends JPanel {

    int a, b;
    int x, y;
    Timer t = new Timer(10, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {//the coordinates of this rectangle 
            a += x;                                 //are mordified in every 10 millisec 
            b += y;                                 //x and y changes the direction of the moving
            repaint();                              //rect. 
        }
    });

    P1() {
        this.getInputMap().put(KeyStroke.getKeyStroke("VK_UP"), "doNothing1");
        this.getActionMap().put("doNothing1", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                x = 0;
                y = -1;
            }
        });
        this.getInputMap().put(KeyStroke.getKeyStroke("VK_LEFT"), "doNothing2");
        this.getActionMap().put("doNothing2", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                x = -1;
                y = 0;
            }
        });
        this.getInputMap().put(KeyStroke.getKeyStroke("VK_DOWN"), "doNothing3");
        this.getActionMap().put("doNothing3", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                x = 0;
                y = 1;
            }
        });
        this.getInputMap().put(KeyStroke.getKeyStroke("VK_RIGHT"), "doNothing4");
        this.getActionMap().put("doNothing4", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                x = -1;
                y = 0;
            }
        });
        t.start();
        setFocusable(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.fillRect(a, b, 10, 10);
    }
}

您使用了错误的InputMap,这是导致问题的主要原因之一。默认的InputMap是在不指定条件的情况下获得的,它使用when_FOCUSED条件,并且由于JPanels默认不允许焦点(实际上不应该强制获得焦点),因此这将不起作用。相反,您将希望使用具有WHEN_IN_FOCUSED_窗口条件的。要执行此操作,必须显式指定。i、 e:

InputMap inputMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); // note condition
inputMap.put(KeyStroke.getKeyStroke("VK_UP"), "doNothing1");
// ....

即使绑定的组件没有焦点,只要它保存在有焦点的顶级窗口中,这也会起作用。

For。在这个(或任何站点)上提问时,您需要花一些精力只发布格式良好的代码。试图理解别人的代码已经够难的了,所以没有必要让我们更难理解。再一次,请修复代码的缩进。谢谢你的帮助。我弄错了。@user2138054:请使用上面的建议显示您最近一次尝试的代码。同样,请只发布格式良好的代码,让我们看看是否可以帮助您。