java - Printing out text when space bar is pressed -


this example of taking place main program. want call method when spacebar pressed want call method example wrote prints out text , changes boolean value isn't working. not sure doing causing not work.

import java.awt.flowlayout; import java.awt.event.keyevent; import java.awt.event.keylistener;  import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextfield;  public class test implements keylistener {      static boolean spacedpressed = false;     static jtextfield text = new jtextfield();     public static void main(string s[]) {          jframe frame = new jframe("jframe test");          jpanel panel = new jpanel();         panel.setlayout(new flowlayout());          jlabel label = new jlabel("test label");          jbutton button = new jbutton();         button.settext("press me");            panel.add(label);         panel.add(button);           frame.add(panel);         frame.setsize(300, 300);         frame.setlocationrelativeto(null);         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setvisible(true);          if(button.getmodel().ispressed()){             system.out.println("pressed");             panel.add(text);             system.out.println(spacedpressed);         }     }      @override     public void keypressed(keyevent e) {         if(e.getkeycode() == keyevent.vk_space){             system.out.println("pressed");             spacedpressed = true;             text.settext("press worked.");         } else {             system.out.println("not pressed");             text.settext("press didn't work.");             spacedpressed = false;         }     }      @override     public void keyreleased(keyevent e) {         // todo auto-generated method stub      }      @override     public void keytyped(keyevent e) {         // todo auto-generated method stub      } } 

so here modified code print key events (i removed button panel, not catching space key events):

public class test_1 implements keylistener {  static boolean spacedpressed = false; static jtextfield text = new jtextfield();  public static void main(string s[]) {      jframe frame = new jframe("jframe test");      jpanel panel = new jpanel();     panel.setlayout(new flowlayout());      jlabel label = new jlabel("test label");      jbutton button = new jbutton();     button.settext("press me");     frame.addkeylistener(new test_1());      panel.add(label);     //      panel.add(button);      frame.add(panel);     frame.setsize(300, 300);     frame.setlocationrelativeto(null);     frame.setdefaultcloseoperation(jframe.exit_on_close);     frame.setvisible(true);      if (button.getmodel().ispressed()) {         system.out.println("pressed");         panel.add(text);         system.out.println(spacedpressed);     } }  @override public void keypressed(keyevent e) {     if (e.getkeycode() == keyevent.vk_space) {         system.out.println("pressed");         spacedpressed = true;         text.settext("press worked.");     } else {         system.out.println("not pressed");         text.settext("press didn't work.");         spacedpressed = false;     } }  @override public void keyreleased(keyevent e) {     system.out.println("released"); }  @override public void keytyped(keyevent e) {     system.out.println("typed"); } } 

and sample output on pressing space key on jframe:

pressed typed released 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -