-1

我试图使用 KeyBinding 移动矩形,但我想我无法正确实现它。我是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);
    }
}
4

1 回答 1

4

您使用了错误的 InputMap,这是造成问题的主要原因之一。当您未指定条件时获得的默认 InputMap 使用 WHEN_FOCUSED 条件,并且由于 JPanel 默认情况下不允许焦点(并且实际上不应该强制获得焦点),因此这不起作用。相反,您将希望使用具有 WHEN_IN_FOCUSED_WINDOW 条件的那个。为此,您必须明确指定这一点。IE:

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

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

于 2013-03-11T01:51:31.020 回答