简体   繁体   中英

CardLayout focus java

I'm trying to get my keyboard input to work, however it is not detecting anything - probably because I'm using CardLayout and i'm not setting the focus properly when I call the show() function.

Currently, it looks like this.

    containerPanel.add(new GameArea(), "newGameArea");
        CardLayout cl = (CardLayout)(containerPanel.getLayout());
        //containerPanel.requestFocusInWindow();
        cl.show(containerPanel, "newGameArea");

Any help would be greatly appreciated.

  • focus is asynchronous, then you have to wrap requestFocus to the invokeLater

  • this should be works in the case that there isn't added another Listenet take Focus

  • don't use KeyListener for Swing JComponents, use KeyBindings instead

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