当在某个 TextField 中按下 F2 时,我一直试图让 JTextArea 显示某个字符串,但目前还没有成功。非常感谢任何帮助。
我的代码可能会揭示我的编程经验有多么少:
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
这有什么用吗?