简体   繁体   English

键绑定 esc 始终有效

[英]key-binding esc to always worrk

so i actually have 2 problems, 1st is hat esc doesnt do anything even tho its the same code as for w, a and s which work, and the 2nd is that when i push the button, none of the keys work (i know its just a label and a button on a frame but this is just a simple example)所以我实际上有两个问题,第一个是 hat esc 没有做任何事情,即使它与 w、a 和 s 的代码相同,第二个是当我按下按钮时,没有一个键工作(我知道它只是一个 label 和一个框架上的按钮,但这只是一个简单的例子)

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

this is where i run it这是我运行它的地方

public class Main{
    public static void main(String[] args ){
        Game game = new Game();
    }
}
label.getInputMap().put(KeyStroke.getKeyStroke("ESC"), "rightAction");

The label on your keyboard means nothing to Java.键盘上的 label 对 Java 没有任何意义。

What is important are the variables defined in the KeyEvent class:重要的是 KeyEvent class 中定义的变量:

VK_ESCAPE

Therefore to create the KeyStroke you use:因此,要创建您使用的 KeyStroke:

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

but still when i click on the button, the keys stop working但是当我点击按钮时,按键停止工作

You are using the default InputMap, which will only work when the component has focus.您正在使用默认的 InputMap,它仅在组件具有焦点时才起作用。 There are 3 InputMaps.有 3 个 InputMap。 Read the Swing tutorial on How to Use Key Bindings .阅读有关如何使用键绑定的 Swing 教程。

You should use the WHEN_ANCESTOR_OF_COMPONENT input map.您应该使用WHEN_ANCESTOR_OF_COMPONENT输入 map。 And the key bindings should be added to the content pane of the frame, not the label.并且键绑定应该添加到框架的内容窗格中,而不是 label。

The code you posted had more than 50 errors, please provide a complete and compileable example.您发布的代码有50多个错误,请提供完整且可编译的示例。 After fixing the errors, I arrived at the following code:修复错误后,我得到以下代码:

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

Then, the problem with "ESC" is that it seems to not recognize that string, however it works when you supply the key code of escape, for example like this:然后,“ESC”的问题是它似乎无法识别该字符串,但是当您提供转义键代码时它可以工作,例如:

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

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

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