编程语言
首页 > 编程语言> > java – 当我点击一个不相关的按钮时,为什么我的键绑定无法工作?

java – 当我点击一个不相关的按钮时,为什么我的键绑定无法工作?

作者:互联网

我创建了一个简单的演示.如果在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");
        }

    }
}

提前谢谢你的帮助.

解决方法:

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.

标签:java,focus,swing,awt,key-bindings
来源: https://codeday.me/bug/20190830/1768966.html