This is a solid "it depends" situation, and this answer is open to plenty of academic discussion.
My Oppinion:
This is definately just an oppinion, but I would recomend talking to your coders and agreeing on two things. Make your decision based on the answers, and put it in the README at the base of your code:
- Anonymous classes, or not?
- Mega classes, or not?
Assuming prefering small, named classes I would go for the following.
public class PublicListener implements KeyAdapter{...}
public class Board extends JPanel {
public Board(){addKeyListener(new PublicListener());
Using KeyAdapter you will only have to override the functions you care about, and other programmers can see quite easily that this is where the input is handled as it is in its own class.
TLDR
Firstly, wanting to work with low level classes is a valid desire. In production code I would not recomend it, but AWT is still the basis of Swing, and wanting to learn is not to be discouraged! If nobody learns the low level stuff, then one day the open source community will have a big problem being built on top of code, which nobody has learned.
While the comments on Swing and Key Bindings are valid, in the documentation about Swing Key Bindings, there is the quote:
An alternative to key bindings is using key listeners. Key listeners have their place as a low-level interface to keyboard input, but for responding to individual keys key bindings are more appropriate and tend to result in more easily maintained code.
So if you are in it to learn some low level stuff, go for it, but take note of the maintainability warnings!
The rest comes down to two main point. Coding Style and Application Architechture.
Coding Style:
Consistency across a code base is often more important than having the perfect implementation, so if you have a codebase with an existing style, then stick to that style. For example, if your codebase already uses lots of Anonymous Classes in other places for other things, then adding a new KeyListener(){...}
is fine. That said, in my personal projects I avoid anonymous classes, because personal preference...
The decision for (what I call) Mega and Mini classes is also important in coding style. The very clear recomendation is to prefer small (mini) classes, with specific roles. This would support implementing a dedicated class which extends KeyAdapter. Mini classes rarely struggle from multiple inheritance problems, allowing you to rely on default implementations (like KeyAdapter), further reducing your code complexity. Future coders can then very easily see what that class is for. Mega classes centralise all of the code into one java file, but then you will be implementing lots of interfaces, and also having to provide default implementations for everything. Compairing KeyListener to KeyAdapter this means implementing three functions instead of overriding one.
Mega classes have their place because multiple inheritance. A "Player" can be a Human, "PositionProvider", a "Drawable" etc. But in all seriousness, this is just not recommended.
Architechture:
Games programming was mentioned, so this is also an important consideration. The architechture of a game can be very different to a dektop GUI application. You need to pick an architechture, and make code which sticks to it. E.g. are you planning to let several components listen to the AWT events and act independantly (entities), or do you plan to have a seperate "InputManager" class, which abstracts input from e.g. keyboard and joypad?