my keyboard controls stop working after click button in graphical gui panel (jpanel) created. below code main class. put keylistener code in controller class. tried set keyboard focus it's not working. have added jpanel class code. tried setting focus on false , it's still not working. can please explain why focus not shifting buttons please?
main class
graphical gui = new graphical(view, getplayer()); frame.add(gui, borderlayout.south); frame.setresizable(false); frame.pack(); frame.setvisible(true); // keyboard focus. frame.requestfocus(); view.addmouselistener(new mousehandler(view)); controller = new controller(world.getplayer()); frame.addkeylistener(controller);
jpanel class
public class graphical extends javax.swing.jpanel { private userview view; private snowman snowman; private game game; public graphical(userview view, snowman snowman) { this.view = view; this.snowman = snowman; initcomponents(); private void jbutton1actionperformed(java.awt.event.actionevent evt) { // todo add handling code here: system.out.println("you playing snowman!"); snowman.changecharacter1(); jbutton1.setfocusable(false); } private void jbutton2actionperformed(java.awt.event.actionevent evt) { // todo add handling code here: system.out.println("you playing jellyfish!"); snowman.changecharacter2(); jbutton2.setfocusable(false); } private void jbutton3actionperformed(java.awt.event.actionevent evt) { // todo add handling code here: system.exit(0); } private void jbutton4actionperformed(java.awt.event.actionevent evt) { // todo add handling code here: new game(); } // variables declaration - not modify private javax.swing.jbutton jbutton1; private javax.swing.jbutton jbutton2; private javax.swing.jbutton jbutton3; private javax.swing.jbutton jbutton4; // end of variables declaration
}
you might want try using key bindings instead of keylistener
if you're having focus issues. times when have lot of swing components component keylistener
registered can lose focus.
i don't know if setfocusable(false)
on buttons cause regain focus of component registered keylistener
know it's not recommended. suppose force focus other components wouldn't scale in long run.
Comments
Post a Comment