简体   繁体   English

使用键绑定

[英]Using Keybinding

I'm doing some very basic coding, just trying to learn the basic concepts behind keybinding. 我正在做一些非常基本的编码,只是试图学习按键绑定的基本概念。 It all seems very straightforward but there's something wrong with my logic or structure that is keeping my code from executing the way I want it to. 一切看起来都非常简单,但是我的逻辑或结构出了点问题,使我的代码无法执行我想要的方式。

Here is my code 这是我的代码

public class Board {

ButtonListener buttonlistener;  
EnterAction enterAction;

public Board(){

    JFrame skeleton = new JFrame();
    skeleton.setDefaultCloseOperation(EXIT_ON_CLOSE);
    skeleton.setVisible(true);
    skeleton.setSize(400, 400);

    buttonlistener = new ButtonListener();
    enterAction = new EnterAction();

    JPanel panel = new JPanel();
    panel.setBackground(Color.BLACK);

    JButton button = new JButton("button");
    button.addActionListener(buttonlistener);
    panel.add(button);
    skeleton.add(panel);        
    panel.getInputMap().put(KeyStroke.getKeyStroke("s"), "doEnterAction");
    panel.getActionMap().put("doEnterAction", enterAction);

}

public class ButtonListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent arg0) {     
        System.out.println("button pressed");
    }       
}

public class EnterAction extends AbstractAction{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("enter pressed");    
    }       
}

public static void main(String[] args){
    new Board();
}

So, it should be pretty simple. 因此,它应该非常简单。 As you can see I'm just trying to make it print out "enter pressed" whenever you press enter, but it isn't printing out anything (unless you click the button also shown in the code above). 如您所见,只要按回车键,我就试图使其“按回车键”打印出来,但是它并没有打印出任何内容(除非您单击上面代码中也显示的按钮)。 Also, in eclipse, the EnterAction class is underlined in yellow, I think it may not be being called right, but I don't know why it wouldn't be. 另外,在日食中,EnterAction类用黄色下划线标出,我认为可能未正确调用它,但我不知道为什么会这样。

Any help is appreciated, thanks. 任何帮助表示赞赏,谢谢。

Change 更改

panel.getInputMap().put(KeyStroke.getKeyStroke("s"), "doEnterAction");

To

panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("S"), "doEnterAction");

Also

skeleton.setDefaultCloseOperation(EXIT_ON_CLOSE);

the parameter must be JFrame.EXIT_ON_CLOSE or just put number 3 . 参数必须为JFrame.EXIT_ON_CLOSE或仅输入数字3

The immediate issue I can see is with the following statement 我看到的直接问题是以下语句

panel.getInputMap().put(KeyStroke.getKeyStroke("s"), "doEnterAction");

KeyStroke.getKeyStroke("s") is going to return null . KeyStroke.getKeyStroke("s")将返回null The requirements for the String passed to this method are very particular and not well documented (IMHO). 传递给此方法的String的要求非常特殊,并且没有详细记录(IMHO)。

You could use KeyStroke.getKeyStroke("S") instead, but I prefer to use KeyStroke.getKeyStroke(KeyEvent.VK_S, 0) as there is no chance of ambiguity in the statement. 您可以改用KeyStroke.getKeyStroke("S") ,但我更喜欢使用KeyStroke.getKeyStroke(KeyEvent.VK_S, 0)因为语句中没有歧义的可能性。

I would also recommend that you define the focus boundaries as well for the input map... 我还建议您也为输入图定义焦点边界...

Instead of panel.getInputMap() , try using panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) to ensure that the key event will be triggered if the window is focused 尝试使用panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)而不是panel.getInputMap()来确保如果焦点panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)窗口,则将触发键事件

Take a look at JComponent#getInputMap for more details. 查看JComponent#getInputMap以获得更多详细信息。

If you haven't already done so, you should also take a look at How to use Key Bindings 如果还没有这样做,还应该看看如何使用键绑定

I think Azad and MadProgrammer are correct, I only had to make one more simple change in addition to what they recommended to get the program running. 我认为Azad和MadProgrammer是正确的,除了他们建议的使程序运行之外,我只需要做一个简单的更改即可。 I have numbered the three items for you as a comment in the code: (copy and paste and you are good to go). 在代码中,我已为您注释了这三个项目:(复制粘贴,您可以使用)。

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class Board {

    ButtonListener buttonlistener;
    EnterAction enterAction;

    public Board() {

        JFrame skeleton = new JFrame();
        //Change #1 below 
        skeleton.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        skeleton.setVisible(true);
        skeleton.setSize(400, 400);

        buttonlistener = new ButtonListener();
        enterAction = new EnterAction();

        JPanel panel = new JPanel();
        panel.setBackground(Color.BLACK);

        JButton button = new JButton("button");
        button.addActionListener(buttonlistener);
        panel.add(button);
        skeleton.add(panel);
        //Change #2 below
        panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                KeyStroke.getKeyStroke("S"), "doEnterAction");
        panel.getActionMap().put("doEnterAction", (Action) enterAction);

    }

    public class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("button pressed");
        }
    }

    public class EnterAction extends AbstractAction {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("enter pressed");
        }
    }

    public static void main(String[] args) {
        new Board();
    }
    //Change #3 below
}

here is the screenshot: 这是屏幕截图: 在此处输入图片说明

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

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