Don't let GUIs implement listeners. This is a bad technique that sadly is taught in way too many tutorials and books. Use a nested class, or even better, instantiate your listeners with a lambda expression. The reason is that by implementing
KeyListener, you're essentially saying that your GUI can be used as an all purpose keystroke handler, even in other classes that it has nothing to do with.
You should also try to avoid extending
JFrame. Your GUI uses a frame, it isn't necessarily a frame itself. I see you already have a
frame member field which is unused right now. Use it.
Your
frame,
panel and
keyToDirection members can be final.
keyToDirection should be a
Map, not a
HashMap. If the mappings don't change between
GUI instances, you can make the
keyToDirection static, and initialize its entries in a static intializer block.
I don't think you should be using an
int[] to represent a direction. Create a
Direction enum instead.
Don't use magic values in your key mapping. Use the constants defined in
KeyEvent instead.
Interaction with Swing components should be done on the Event Dispatch
Thread:
Try to avoid creating new components after the GUI has been made visible. Instantiate your labels when you create the GUI, and just change their text value during the course of a game. You can then get rid of the call to validate(). Even if you don't go this route, you should call
revalidate() instead of
validate().
If you post more of your code instead of linking to it, we can offer some more advice on that as well.