[英]Java get action command with a keyTyped event
I have a JTextField to which I set an action command with the method setActionCommand(String s). 我有一个JTextField,可以使用setActionCommand(String s)方法向其设置操作命令。 It seems I can only get this action command when I use an actionPerformed event. 看来我只有在使用actionPerformed事件时才能获得此action命令。
However I need to get some signature from my JTexteField with a keyTyped event, because I use several JTextFields, and I need to distinguish in which one the Enter key has been typed. 但是,我需要通过keyTyped事件从JTexteField中获取一些签名,因为我使用了多个JTextField,并且需要区分Enter键是在哪一个中键入的。 Here is my snippet for the keyTyped event: 这是我的keyTyped事件的代码段:
public void keyTyped(KeyEvent k) {
String id = k.getComponent().getClass().getSimpleName();
if (KeyEvent.VK_ENTER == k.getKeyChar() && "JTextField".equals( id )) {
JTextField tempTxt = (JTextField) k.getComponent();
// Here I would like to get my JTextField signature, such as k.getActionCommand() with actionPerformed() method
}
}
How can I do that? 我怎样才能做到这一点?
您可以仅在每个JTextField
上使用setName(String name)
,然后可以通过调用k.getComponent().getName()
来区分它们k.getComponent().getName()
However I need to get some signature from my JTexteField with a keyTyped event, because I use several JTextFields, and I need to distinguish in which one the Enter key has been typed. 但是,我需要通过keyTyped事件从JTexteField中获取一些签名,因为我使用了多个JTextField,并且需要区分Enter键是在哪一个中键入的。 Here is my snippet for the keyTyped event: 这是我的keyTyped事件的代码段:
use ActionListener added to JTextField instead of KeyListener 使用添加到JTextField而不是KeyListener的 ActionListener
for JTextComponents
to use DocumentListener/DocumentFilter 让JTextComponents
使用DocumentListener / DocumentFilter
then easiest of ways could be 那么最简单的方法可能是
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.