Simply implementing KeyListener
isn't enough, you need to specify which component you want to receive key events.
This is where the problems start. KeyListener
will only raise events for components that have focus AND are focusable.
A better solution would be to use the key bindings API which provides you with better control over the level of focus a component needs before it will trigger key events
You should avoid using setBounds
for, at least, two reasons.
Firstly, the container you adding the component to is under the control of a layout manager (BorderLayout
in this instance), which makes the use of setBounds
pointless and secondly, you don't control the factors which will alter the required amount of space a component might need when presented on different platforms, such as font metrics and rendering pipelines. Let the layout managers do there job
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…