java - Better JLabel movement with KeyListeners -
i'm having slight problem movement of jlabel using keylisteners. when click key move label, moves little, pauses second, moves. how can make movement more smooth?
frame.addkeylistener(new keyadapter(){ public void keypressed(keyevent e) { if(e.getkeychar() == 'w'){ movey -= 10; label.setlocation(movex, movey); } if(e.getkeychar() == 'a'){ movex -= 10; label.setlocation(movex, movey); } if(e.getkeychar() == 's'){ movey += 10; label.setlocation(movex, movey); } if(e.getkeychar() == 'd'){ movex += 10; label.setlocation(movex, movey); } } });
jframe
default never reactkeyevent
listenedkeylistener
jframe
isn't focusablejcomponent
, need use focusable contianer e,g,jpanel
, again wrong decision, because required set pernament focus -setfocusable(true)
don't use
keylistener
swing jcomponents
, use hight possible abstraction, usekeybindings
(search code examples here)
Comments
Post a Comment