簡體   English   中英

將KeyListener添加到JCombobox編輯器和JCombobox本身之間有什么區別

[英]What is the difference between adding a KeyListener to JCombobox editor and JCombobox itself

    listItemsCombo.getEditor().getEditorComponent().addKeyListener(new keyPressListener()); //Works correct

這很好用。 但是,在將keyListener添加到jcomboBox時,按下“Enter”時keyPress沒有得到重新識別。 將keyListener添加到jComboxBox並將監聽器添加到其編輯器之間的確切區別是什么?何時將監聽器添加到編輯器?

   listItemsCombo.addKeyListener(new KeyPressListener())//When should we use this?

將keyListener添加到jComboxBox並向其編輯器添加偵聽器之間的確切區別是什么?

KeyEvents僅分派給具有焦點的組件。

何時將監聽器添加到編輯器?

您不應在組合框或編輯器上使用KeyListener。 Swing旨在與Key Bindings一起使用。 使用鍵綁定,即使組件沒有焦點(如果您願意),也可以處理KeyStroke 有關更多信息,請閱讀Swing教程中有關如何使用鍵綁定的部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM