繁体   English   中英

按下按键时更改图片

[英]Making a picture change when a key is pressed

我目前有我的键盘按钮工作。 我想知道按方向键是否可以在 2 张图片之间切换。 我的照片被放在一个名为 thing 的 JLabel 中。 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);
    }
}

}

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

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();
                    }
                }

因此,当按下键时,会弹出一个图像,并在重新设置时更改。 我还使用了一个变量,我创建了一个变量,它可以在图片之间进行更改,以使其像通过在图片之间交替移动而使人行进一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM