Java密钥绑定无法成功实现

时间:2017-12-28 05:38:28

标签: java binding key

以下是我的项目

public class KeyStrokeExample {

private JFrame jFrame;
private JPanel jPanel;
private JButton jButton;

private Action action;
public static void main(String[] args) {
    KeyStrokeExample keyStrokeExample=new KeyStrokeExample();
    keyStrokeExample.initLayout();
    keyStrokeExample.test();
}

private void initLayout() {
    jFrame=new JFrame("frame");
    jPanel=new JPanel();
    jButton=new JButton("test");

    jPanel.add(jButton);
    jFrame.add(jPanel);

    jFrame.pack();
    jFrame.setVisible(true);
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private void test() {
    action=new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            System.out.println("test");
        }
    };
    int condition = JComponent.WHEN_FOCUSED;
    InputMap inputMap = jPanel.getInputMap(condition);
    ActionMap actionMap = jPanel.getActionMap();

    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_DOWN_MASK ),"DELETE");
    actionMap.put( "DELETE", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {

            System.out.println("test");

        }
    });
}
}

我从https://tips4java.wordpress.com/2008/10/10/key-bindings/

练习

我不知道为什么程序不打印“测试”

当我删除

    action=new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            System.out.println("test");
        }
    };

它可以工作打印测试“

我想了解发生了什么

由于

0 个答案:

没有答案