按下键时更改图片

时间:2019-12-11 16:58:16

标签: java keyboard

我目前可以使用键盘按钮。我想知道按方向键时是否可以在两张图片之间切换。我的图片放在JLabel中,称为Thing。 JLabel正在移动,我所要做的就是使其移动,以便在按下某个键时图片交替显示。

class start {

start() {
    JFrame mainFrame = new JFrame();
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setSize(1210, 700);
    mainFrame.setLocation(new java.awt.Point(150, 30));
    mainFrame.setLayout(null);
    mainFrame.setFocusable(true);
    mainFrame.setFocusTraversalKeysEnabled(true);
    mainFrame.setIconImage(new ImageIcon("images\\sword.png").getImage());
    JLabel thing = new JLabel();
    thing.setIcon(new ImageIcon("image\\walkdown.png"));
    thing.setBounds(300, 300, thing.getPreferredSize().width, thing.getPreferredSize().height);

    InputMap inputMap = thing.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap actionMap = thing.getActionMap();

    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), "move.up");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, false), "move.up");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "move.down");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, false), "move.down");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), "move.left");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "move.left");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "move.right");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "move.right");

    actionMap.put("move.up", new ThingAction(thing, new Point(0, -5)));



    actionMap.put("move.down", new ThingAction(thing, new Point(0, 5)));
    actionMap.put("move.left", new ThingAction(thing, new Point(-5, 0)));
    actionMap.put("move.right", new ThingAction(thing, new Point(5, 0)));

    mainFrame.add(thing);
    mainFrame.setVisible(true);
}

public class ThingAction extends AbstractAction {

    private JLabel thing;
    private Point delta;

    public ThingAction(JLabel thing, Point delta) {
        this.thing = thing;
        this.delta = delta;
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        thing.setLocation(thing.getX() + delta.x, thing.getY() + delta.y);
    }
}

}

1 个答案:

答案 0 :(得分:1)

我通过使用稍微不同的方式解决了这个问题。我认为它可能效率较低,但可以。 我创建了

void keylisteners() {
        MainFrame.addKeyListener(new java.awt.event.KeyListener() {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
                    if (walking == true && direction != 0) {
                        MT.cancel();
                    }

public void keyTyped(KeyEvent e) {
            }

            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
                    if (direction == 0) {
                        thing.setIcon(new ImageIcon("images\\walk0b.png"));
                        MT.cancel();
                    }
                }

因此,当按下键时,会弹出图像,并且在释放键时会更改图像。我还使用的方向是一个变量,我创建了这个变量以在图片之间进行更改,以使cahrecter在图片之间交替显示。