简体   繁体   中英

Adding keylistener to panel in CardLayout

I'm writing a simple game, and I have main frame with 4 JPanels placed in CardLayout. Main frame looks like that:

private static JPanel[] panele = new JPanel[4];
private static JPanel panel;    
public GameWindow()
{
    super("Sokoban");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    panele[0] = new MainMenu();
    panele[1] = new LoadGameMenu();
    panele[2] = new SaveGameMenu();     
    panele[3] = new GameScene();        
    panel = new JPanel(new CardLayout());

    //((MainMenu)panele[0]).setSaveOptionState(false);
    panel.add(panele[0], "MainMenu");
    panel.add(panele[1], "LoadGameMenu");
    panel.add(panele[2], "SaveGameMenu");
    panel.add(panele[3], "GameScene");      
    add(panel, BorderLayout.CENTER);


}

The GameScene panel have react to keyboard input. First I tried keylistener:

public GameScene() {        
    setFocusable(true);
    initWorld(); //Drawing on JPanel takes place here       
    addKeyListener(new Keyboard());         
}
class Keyboard extends KeyAdapter
{
    private int key;
    public void keyPressed(KeyEvent event)
    {
        System.out.println("Tu jestem");
        key = event.getKeyCode();
        if(key == KeyEvent.VK_ESCAPE)
        {
            Game.gra = new GameWindow(MenuAction.MAIN_MENU);
            System.out.println("Escape");
        }

That wasn't working... so I tried keybinding (simple implementation):

public GameScene() {    

    setFocusable(true);
    initWorld();  //Drawing on JPanel takes place here      
   // requestFocus();
    setInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, getInputMap());
    KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
    getInputMap().put(key, "pressed");      
    getActionMap().put("pressed", new AbstractAction(){         
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("Spacja");
            //Game.gra = new GameWindow(MenuAction.MAIN_MENU);

        }

    });
}

It's still not working... I tried adding requestFocus and requestFocusInWindow() but with no effect. Any ideas how to fix it or to do it?

Solution have been found. In key binding I should write:

getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(key, "pressed");

insted of:

getInputMap().put(key, "pressed"); 

Try this:

setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW));

Edit: For reference , "Each JComponent has one action map and three input maps."

By using Action , illustrated here , you can bind a key (or combination) to that Action , as shown here . For additional guidance, please edit your question to include an sscce using either or both examples.

I don't know if this is what you really want but if you want to simply react to key event when you're in "GameScene" card you should switch to this card firstly (to grab focus):

        CardLayout cl = new CardLayout();
        panel = new JPanel(cl);
        ...
        add(panel, BorderLayout.CENTER);
        cl.show(panel, "GameScene");

Also looking at your code - consider using JMenu and/or JTabbedPane .

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