How to detect a key press in Java

前端 未结 6 1630
野的像风
野的像风 2020-12-30 07:29

My son (aged 11) who is learning Java is going to enter a question and some code. He tells me he has not been able to find the answer to this question on the site. You will

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 08:10

    I don't see anything in your code which actually listens for key presses or which modifies the position of x and y. The circle will only move if you change x and y and then repaint the screen.

    You have set up the panel, etc here but not actually added anything to respond to user action. You will probably need to add a KeyListener somewhere to respond to key presses. This is probably either in the tutorial you are doing or in a previous or later tutorial.

提交回复
热议问题