JPanel doesn't response to KeyListener event

前端 未结 4 716
深忆病人
深忆病人 2020-12-06 18:28

I have a subclass of JFrame that uses a class extended from JPanel

public class HelloWorld extends JPanel implements KeyListener
         


        
4条回答
  •  孤街浪徒
    2020-12-06 19:11

    JPanel is not Focusable by default. That is, it can not respond to focus related events, meaning that it can not respond to the keyevents.

    I would suggest trying to setFocusable on the pane to true and trying again. Make sure you click the panel first to make sure it receives focus.

    Understand though, you WILL get strange focus traversal issues, as the panel will now receive input focus as the user navigates through your forms, making it seem like the focus has been lost some where.

    Also, KeyListeners tend to be unreliable in this kind of situation (due to the way that the focus manager works).

提交回复
热议问题