如何键绑定ESC

how to key-bind ESC

我的问题是 ESC 不执行任何操作,即使它与 w、a 和 s 的代码相同,但当我按下按钮时,w、a 和 s 不再起作用。这段代码应该做的是显示带有 JLabel 的 JFrame,当我分别按 w、s、a 和 ESC 时,它应该向上、向下、向左和向右移动。这是我用作指南的视频(我并不是说这是一个糟糕的视频)https://www.youtube.com/watch?v=IyfB0u9g2x0&t=118s

Game(){
    
        frame = new JFrame("KeyBinding Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(420,420);
        frame.setLayout(null);
        
        label = new JLabel();
        label.setBackground(Color.red);
        label.setBounds(100, 100, 100, 100);
        label.setOpaque(true);
        b = new JButton();
        b.setBackground(Color.red);
        b.setBounds(400, 400, 400, 400);
        b.setOpaque(true);
        upAction = new UpAction();
        downAction = new DownAction();
        leftAction = new LeftAction();
        rightAction = new RightAction();
        
        label.getInputMap().put(KeyStroke.getKeyStroke('w'), "upAction");
        label.getActionMap().put("upAction", upAction);
        label.getInputMap().put(KeyStroke.getKeyStroke('s'), "downAction");
        label.getActionMap().put("downAction", downAction);
        label.getInputMap().put(KeyStroke.getKeyStroke('a'), "leftAction");
        label.getActionMap().put("leftAction", leftAction);
        label.getInputMap().put(KeyStroke.getKeyStroke("ESC"), "rightAction");
        label.getActionMap().put("rightAction", rightAction);
        frame.add(b);
        frame.add(label);
        frame.setVisible(true);
    }
    
    public class UpAction extends AbstractAction{

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setLocation(label.getX(), label.getY()-10);
        }       
    }
    public class DownAction extends AbstractAction{

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setLocation(label.getX(), label.getY()+10);   
        }       
    }
    public class LeftAction extends AbstractAction{

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setLocation(label.getX()-10, label.getY());   
        }       
    }
    public class RightAction extends AbstractAction{

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setLocation(label.getX()+10, label.getY());
        }       
    }
}

这是我运行它

的地方
public class Main{
    public static void main(String[] args ){
        Game game = new Game();
    }
}

您发布的代码有 50 多个错误,请提供完整且可编译的示例。修复错误后,我得到了以下代码:

import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;

class Game
{

final JLabel label;

    
Game()
{       
        var frame = new JFrame("KeyBinding Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(420,420);
        frame.setLayout(null);
        
        label = new JLabel();
        label.setBackground(Color.red);
        label.setBounds(100, 100, 100, 100);
        label.setOpaque(true);
        
        var b = new JButton();
        b.setBackground(Color.red);
        b.setBounds(400, 400, 400, 400);
        b.setOpaque(true);
        var upAction = new UpAction();
        var downAction = new DownAction();
        var leftAction = new LeftAction();
        var rightAction = new RightAction();
        
        label.getInputMap().put(KeyStroke.getKeyStroke('w'), "upAction");
        label.getActionMap().put("upAction", upAction);
        label.getInputMap().put(KeyStroke.getKeyStroke('s'), "downAction");
        label.getActionMap().put("downAction", downAction);
        label.getInputMap().put(KeyStroke.getKeyStroke('a'), "leftAction");
        label.getActionMap().put("leftAction", leftAction);
        label.getInputMap().put(KeyStroke.getKeyStroke("ESC"), "rightAction");
        label.getActionMap().put("rightAction", rightAction);
        frame.add(b);
        frame.add(label);
        frame.setVisible(true);
    }
    
    public class UpAction extends AbstractAction{

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setLocation(label.getX(), label.getY()-10);
        }       
    }
    public class DownAction extends AbstractAction{

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setLocation(label.getX(), label.getY()+10);   
        }       
    }
    public class LeftAction extends AbstractAction{

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setLocation(label.getX()-10, label.getY());   
        }       
    }
    public class RightAction extends AbstractAction{

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setLocation(label.getX()+10, label.getY());
        }       
    }

    public static void main(String[] args ){
        Game game = new Game();
    }
}

然后,“ESC”的问题是它似乎无法识别该字符串,但是当您提供转义键代码时它会起作用,例如:

import java.awt.event.KeyEvent;
[...]
label.getInputMap().put(KeyStroke.getKeyStroke((char) KeyEvent.VK_ESCAPE), "rightAction");
label.getInputMap().put(KeyStroke.getKeyStroke("ESC"), "rightAction");

键盘上的标签对 Java 没有任何意义。

重要的是KeyEvent中定义的变量class:

VK_ESCAPE

因此要创建您使用的 KeyStroke:

label.getInputMap().put(KeyStroke.getKeyStroke("ESCAPE"), "rightAction");

but still when i click on the button, the keys stop working

您正在使用默认的 InputMap,它仅在组件具有焦点时才起作用。有 3 个 InputMap。阅读 How to Use Key Bindings 上的 Swing 教程。

您应该使用 WHEN_ANCESTOR_OF_COMPONENT 输入地图。键绑定应添加到框架的内容窗格,而不是标签。