简体   繁体   中英

Java cardlayout, KeyListener stops working

I have KeyListener in my CardLayout .

KeyListener listener = new MyKeyListener();
addKeyListener(listener);
setFocusable(true);

And it works just fine. The problem is when I exit this JPanel from my main screen manager:

private CardLayout cl;
cl.show(cardPanel, "1");

and comes back to it the KeyListener stops working. How can I evoke the KeyListener again, when the JPanel is shown?

KeyListeners have issues with focus, while you have set the component focusable, you've not requestFocusInWindow when the view has changed back.

Instead, use Key Bindings which have greater focus handling control

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM