单击不相关的按钮时,为什么我的键绑定无法正常工作?

时间:2014-08-20 11:03:45

标签: java swing focus awt key-bindings

我创建了一个简单的演示。如果在mainPanel上触发特定键Stroke事件,将显示消息“a is typed”。但是,按下主面板下面的按钮后它不起作用。 这是KeybindingTest类及其内部类:

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class KeybindingTest {
    private JFrame frame;
    private MainPanel mainPanel;
    private ButtonPanel buttonPanel;
    public static void main(String[] args) {
        KeybindingTest test = new KeybindingTest();
        test.createUI();
    }

    public void createUI(){
        frame = new JFrame("Keybing Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainPanel = new MainPanel();
        buttonPanel = new ButtonPanel();
        frame.add(mainPanel,BorderLayout.CENTER);
        frame.add(buttonPanel,BorderLayout.SOUTH);
        KeybindingListener.getInstance().keybinding(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    @SuppressWarnings("serial")
    class MainPanel extends JPanel{
        @Override
        public Dimension getPreferredSize() {
            // TODO Auto-generated method stub
            return new Dimension(400, 300);
        }
    }

    @SuppressWarnings("serial")
    class ButtonPanel extends JPanel{
        public ButtonPanel(){
            setBackground(Color.green);
            JButton enableButton = new JButton("enable");
            enableButton.addActionListener(new EnableButtonListener());
            JButton disableButton = new JButton("disable");
            disableButton.addActionListener(new DisableButtonListener());
            add(enableButton);
            add(disableButton);
        }
    }

    class EnableButtonListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            System.out.println("enabled");
        }

    }

    class DisableButtonListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            System.out.println("disabled");
        }

    }
}

这是KeybindingListener类及其内部类:

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class KeybindingListener {
    private static KeybindingListener keybindingListener;
    private final String mapKey = "a";

    private KeybindingListener(){
        //singleton
    }

    public static KeybindingListener getInstance(){
        if (keybindingListener == null) {
            keybindingListener = new KeybindingListener();
            return keybindingListener;
        }else {
            return keybindingListener;
        }
    }

    public void keybinding(JPanel mainPanel){
        mainPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_A , 0),mapKey);
        mainPanel.getActionMap().put(mapKey, new keyAction());
    }

    @SuppressWarnings("serial")
    class keyAction extends AbstractAction{

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            System.out.println("a is typed");
        }

    }
}

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:5)

正如this answer中所述,JComponent#getInputMap()JComponent#getInputMap(WHEN_FOCUSED)的便捷方法。由于您要将密钥绑定添加到JPanel,这意味着您的面板必须具有正确工作的焦点,这是不可能的。

您可以将键绑定原样添加到面板中的所有组件,也可以将键绑定添加到面板中,如下所示:

public void keybinding(JPanel mainPanel){
    mainPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_A , 0),mapKey);
    mainPanel.getActionMap().put(mapKey, new keyAction());
}

有关此问题的更好解释,请参阅How Key Bindings Work