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);         }     } }); 

  1. jframe default never react keyevent listened keylistener

  2. jframe isn't focusable jcomponent, need use focusable contianer e,g, jpanel, again wrong decision, because required set pernament focus - setfocusable(true)

  3. don't use keylistener swing jcomponents, use hight possible abstraction, use keybindings (search code examples here)


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -