【问题标题】:Action listener, key board input动作监听器,键盘输入
【发布时间】:2014-01-18 07:25:16
【问题描述】:

我遇到了动作监听器的问题。 问题是这样的:

 private static void createAndShowGUI() {
    int i;
    System.out.println("Created GUI on EDT? "+
    SwingUtilities.isEventDispatchThread());
    JFrame f = new JFrame("Best Game Ever");        
    JButton buttonA = new JButton("Press 'z'");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.add(new MyPanel(),BorderLayout.CENTER);
    f.add(buttonA, BorderLayout.PAGE_END);      
    f.pack();
    f.setVisible(true);
    final char ACTION_KEY = 'z';
    final char ACTION_KEY2 ='x';
    Action actionListener = new AbstractAction()
    {          
        public void actionPerformed(ActionEvent actionEvent)
        {
        }
    };

    Action actionListener2 = new AbstractAction()
    {          
        public void actionPerformed(ActionEvent actionEvent)
        {
            System.out.println("x");
        }
    };      
    KeyStroke z = KeyStroke.getKeyStroke('z');       
    InputMap inputMap = buttonA.getInputMap();
    inputMap.put(z, ACTION_KEY);
    ActionMap actionMap = buttonA.getActionMap() ;
    actionMap.put(ACTION_KEY, actionListener);        
    KeyStroke x = KeyStroke.getKeyStroke('x');
    InputMap inputMap2 = buttonA.getInputMap();
    inputMap2.put(x, ACTION_KEY2);
    ActionMap actionMap2 = buttonA.getActionMap();
    actionMap2.put(ACTION_KEY2, actionListener2);        
} 

我只是无法为它编写一个代码,即使我拿着钥匙也只打印一次 x。 谢谢。

很抱歉没有把所有的代码:(

希望它现在有意义。

【问题讨论】:

  • 您的操作在哪里触发???
  • 你遇到了什么问题?

标签: java swing action actionlistener


【解决方案1】:

“我只是无法为它编写一个代码,即使我按住键也只打印一次。”

一种方法是在kestroke中添加released,它只会在按键被释放时打印

   inputMap.put(KeyStroke.getKeyStroke("released X"), "printX");

KeyStroke 表示键盘或等效输入设备上的键操作。 KeyStrokes 只能对应特定键的按下或释放,就像 KEY_PRESSED 和 KEY_RELEASED KeyEvents 一样;或者,它们可以对应于键入特定的 Java 字符,就像 KEY_TYPED KeyEvents 所做的那样。

Here's an example
import java.awt.event.ActionEvent;
import javax.swing.*;

public class TestKeyBind {

    public TestKeyBind() {
        JPanel panel = new JPanel();

        Action actionListener2 = new AbstractAction() {
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println("x");
            }
        };
        InputMap inputMap = panel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = panel.getActionMap();

        inputMap.put(KeyStroke.getKeyStroke("released X"), "printX");
        actionMap.put("printX", actionListener2);

        JFrame frame = new JFrame();
        frame.add(panel);
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new TestKeyBind();
            }
        });
    }
}

您也可以尝试typed,但“key typed”事件会在每个平台上运行。在windows上不行,我刚试过

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2021-02-11
    • 1970-01-01
    • 2020-05-24
    • 1970-01-01
    相关资源
    最近更新 更多