[英]Java get action command with a keyTyped event
我有一個JTextField,可以使用setActionCommand(String s)方法向其設置操作命令。 看來我只有在使用actionPerformed事件時才能獲得此action命令。
但是,我需要通過keyTyped事件從JTexteField中獲取一些簽名,因為我使用了多個JTextField,並且需要區分Enter鍵是在哪一個中鍵入的。 這是我的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
}
}
我怎樣才能做到這一點?
您可以僅在每個JTextField
上使用setName(String name)
,然后可以通過調用k.getComponent().getName()
來區分它們k.getComponent().getName()
但是,我需要通過keyTyped事件從JTexteField中獲取一些簽名,因為我使用了多個JTextField,並且需要區分Enter鍵是在哪一個中鍵入的。 這是我的keyTyped事件的代碼段:
使用添加到JTextField而不是KeyListener的 ActionListener
讓JTextComponents
使用DocumentListener / DocumentFilter
那么最簡單的方法可能是
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.