Okay so long story short, here is what is happening and what I am trying to do.

  1. I have a class called GameGUI, this is a JFrame.
  2. This JFrame is populated with tiles(JLabels with ImageIcons)
  3. I use my arrow keys to move my guy around the JFrame (just updates the JLabel Images)
  4. I made a settings option, this is a new JFrame that I setVisbile(true), make changes, then setVisible(false)
  5. After I setVisible(false) my arrow keys no longer make my character move on GameGUI.

I have tried the following, oh which none work: (all guesses based off Googling my problem)

GameGUI gg = new GameGUI();
gg.setFocusable(true);
gg.addKeyListener(null);
gg.requestFocusInWindow();

I cannot seem to find a way to get my KeyListener to move back to the GameGUI after I open (make visible) this settings menu then close it (make invisible). I do have radio buttons within the settings menu which is why I believe it gains focus, due to a physical mouse click.

Any help on getting focus back onto GameGUI would be much appreciated!

Thanks!

有帮助吗?

解决方案

5.After I setVisible(false) my arrow keys no longer make my character move on GameGUI.

Don't use a KeyListener. Swing was designed to be used with Key Bindings.

See Motion Using the Keyboard, which will explain your probable problem and give the solution using Key Bindings.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top