【问题标题】:ActionMap / InputMap to make JTextArea display "this text" when F2 is pressedActionMap / InputMap 使 JTextArea 在按下 F2 时显示“此文本”
【发布时间】:2013-10-30 19:25:18
【问题描述】:

我一直在尝试让 JTextArea 在某个 TextField 中按下 F2 时显示某个字符串,但目前还没有成功。非常感谢任何帮助。

我的代码可能会揭示我的编程经验有多么少:

final String ACTION_KEY = "this text";

public void actionPerformed(ActionEvent actionEvent) {

                JTextField source = (JTextField) actionEvent.getSource();

                System.out.println("Activated: " + source.getText());

                textAreaInstructions.setText("this text");

              }
            };


            KeyStroke F2 = KeyStroke.getKeyStroke("F2");
            InputMap inputMap = timeStep.getInputMap();
            inputMap.put(F2, ACTION_KEY);
            ActionMap actionMap = timeStep.getActionMap();          
            actionMap.put(ACTION_KEY, actionListener);

编辑:我现在正在尝试使用此代码:

InputMap inputMap = timeStep.getInputMap();
        Object actionSubmit = inputMap.get(KeyStroke.getKeyStroke("ENTER"));
        Object actionSubmitSp = inputMap.get(KeyStroke.getKeyStroke("SPACE"));
        System.out.println("actionSubmit for space = " + actionSubmitSp);
        ActionMap actionMap = timeStep.getActionMap();
        Action action = actionMap.get(actionSubmit);
        System.out.println("actionSubmit = " + actionSubmit);
        timeStep.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),
                actionSubmit);

编辑:

这会打印出来

actionSubmit for space = null
actionSubmit = notify-field-accept

这有用吗?

【问题讨论】:

  • 您是否需要始终刷新 JTextArea 中的文本?还是只需要按键刷新?
  • 只能按 F2 (嗯,这是我实际上想要的选项卡,但我知道可能还有其他与选项卡有关的问题,所以我想我会先尝试 F2)
  • 我不明白你的问题是什么?您在使用 KeyBindings 时遇到问题吗?尝试阅读此How to use KeyBindings
  • 我的问题与发布的代码无关。那是我将文件的备份保存在与原始文件相同的包中并且忘记更改代码,因此正在实施备份而不是更新的原始文件。这花了我很多时间。哈哈。也就是说,我还没有确定我确实了解如何进行这项工作:当我发布这篇文章时,我已经在电脑上呆了很长时间,而我才刚刚回到网上。 +1 为您提供帮助。
  • 问题已解决:详见答案。

标签: java swing jtextfield key-bindings key-events


【解决方案1】:

问题与发布的代码无关。那是我将文件的备份保存在与原始文件相同的包中并且忘记更改代码,因此正在实施备份而不是更新的原始文件。这花了我很多时间。哈哈。

编辑:所以无论如何,既然我知道我正在运行哪个文件,我发现下面的代码(我在这里得到:http://blog.marcnuri.com/blog/.../2007/06/06/Detecting-Tab-Key-Pressed-Event-in-JTextField-s-Event-VK-TAB-KeyPressed)做了我想要的(对于 tab 而不是 F2,但显然可以工作也适用于 F2,在这种情况下不需要第一行):

timeStep.setFocusTraversalKeys(
                KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);

        timeStep.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_TAB){
                    instruction = "tab pressed";
                    textAreaInstructions.setText(instruction);
                    lblTabEvent.setText(instruction);
                    // If you want to change the focus to the next component 
                    timerInterval.grabFocus();
            }
                else {

                    textAreaInstructions.setText("got here, "+ e.getKeyCode());
                }
            }

        });

【讨论】:

  • 这一点是。我是否会因为选项卡的处理方式有所不同或其他原因而遇到更多问题,我还不知道。
  • 尝试使用setFocusTraversalKeysEnabled() 在你的领域钩VK_TAB
  • +1 寻求帮助 谢谢:我看过与标签相关的帖子。我只是先尝试​​ F2 或空间。我现在使用一些不同的代码(见编辑)。实际上,我不确定是否使用原始代码或编辑代码解决了上述问题。它正在打印 actionSubmit for space = null
  • 下一个代码为我打印 actionSubmit for space = SPACE1KeyStroke F2 = KeyStroke.getKeyStroke("SPACE"); InputMap inputMap = area.getInputMap(); inputMap.put(F2, "SPACE1"); Object actionSubmit = inputMap.get(F2); System.out.println("actionSubmit for space = " + actionSubmit);
  • 我想知道为什么它打印出来的不一样?我的评论没有显示它打印的所有内容:我将把它放在问题的另一个编辑中。 +1 为您提供帮助。
猜你喜欢
  • 2017-07-31
  • 1970-01-01
  • 2016-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-05
  • 2012-05-27
  • 2017-09-03
相关资源
最近更新 更多