Wednesday, 15 May 2013

java - Displaying keypress count in JTextField or Jbutton -



java - Displaying keypress count in JTextField or Jbutton -

i'm having problem displaying amount of times key has been pressed , display number in jtextfield or jbutton. give me hand that?

i have keyevent.vk_numpad3 displayed, seems keyevent.vk_2 having such problem with:

i can display number doesn't update each time key pressed.

here total code.. bit big asked it.. :)

public class display extends jframe{ public static final int canvas_width = 1000; public static final int canvas_height = 450; public static final color line_color = color.black; public static final color ground_color = color.green; public static final color canvas_background = color.cyan; public static final int trans = color.opaque; private int x1 = canvas_width / 2; private int y1 = canvas_height / 2; private int x2 = canvas_width /2; private int y2 = canvas_height / 2; private int sx1 = canvas_width ; private int sy1 = canvas_height ; private int sy2 = canvas_height ; private int rx1 = canvas_width - canvas_width; private int ry1 = canvas_height; private int rx2 = canvas_width; private int ry2 = canvas_height ; private int lx1 = canvas_width / 2; private int ly1 = canvas_height / 2; private int lx2 = canvas_width / 2; private int ly2 = canvas_height / 2; private int mx1 = canvas_width / 2; private int my1 = canvas_height / 2; private int mx2 = canvas_width / 2; private int my2 = canvas_height / 2; int[] xs = {380, 460, 460, 540, 540, 620, 500, 380}; int[] ys = {260, 260, 250, 250, 260, 260, 205, 260}; private drawcanvas canvas; private jtextfield altitude; private jtextfield taspeed; private jlabel altbutton; private int counta = 0; private int counts = 0; private int bcount1 = 0; public string ccount = integer.tostring(bcount1); public class countupaltitude extends abstractaction { /** constructor */ public countupaltitude(string name, string shortdesc, integer mnemonic) { super(name); putvalue(short_description, shortdesc); putvalue(mnemonic_key, keyevent.vk_3); bcount1 += 1; } //right @override public void actionperformed(actionevent e) { addkeylistener(new keyadapter() { public void keypressed(keyevent evt) { switch(evt.getkeycode()) { case keyevent.vk_numpad6: //original count counta += 5; altitude.settext(counta + ""); } } }); } } public class countdownaltitude extends abstractaction { /** constructor */ public countdownaltitude(string name, string shortdesc, integer mnemonic) { super(name); putvalue(short_description, shortdesc); putvalue(mnemonic_key, keyevent.vk_4); } //right @override public void actionperformed(actionevent e) { addkeylistener(new keyadapter() { public void keypressed(keyevent evt) { switch(evt.getkeycode()) { case keyevent.vk_numpad3: //original count counta -= 5; altitude.settext(counta + ""); } } }); } } public class countuptaspeed extends abstractaction { /** constructor */ public countuptaspeed(string name, string shortdesc, integer mnemonic) { super(name); putvalue(short_description, shortdesc); putvalue(mnemonic_key, keyevent.vk_1); } //left @override public void actionperformed(actionevent e) { addkeylistener(new keyadapter() { public void keypressed(keyevent evt) { switch(evt.getkeycode()) { case keyevent.vk_numpad4: //original count counts += 5; taspeed.settext(counts + ""); } } }); } } public class countdowntaspeed extends abstractaction { /** constructor */ public countdowntaspeed(string name, string shortdesc, integer mnemonic) { super(name); putvalue(short_description, shortdesc); putvalue(mnemonic_key, keyevent.vk_2); } //left @override public void actionperformed(actionevent e) { addkeylistener(new keyadapter() { public void keypressed(keyevent evt) { switch(evt.getkeycode()) { case keyevent.vk_numpad1: //original count counts -= 5; taspeed.settext(counts + ""); } } }); } } public display() { canvas = new drawcanvas(); canvas.setpreferredsize(new dimension(canvas_width, canvas_height)); canvas.setlayout(new borderlayout()); container cp = getcontentpane(); cp.setlayout(new borderlayout()); // create actions shared button , keys action countupaltitude = new countupaltitude("count up", "", new integer(keyevent.vk_enter)); action countdownaltitude = new countdownaltitude("count down", "", new integer(keyevent.vk_d)); action countuptaspeed = new countuptaspeed("count up", "", new integer(keyevent.vk_enter)); action countdowntaspeed = new countdowntaspeed("count down", "", new integer(keyevent.vk_d)); height = new jtextfield("0", 5); altitude.sethorizontalalignment(jtextfield.center); altitude.seteditable(false); altitude.setopaque(false); altitude.setfont(altitude.getfont().derivefont(25f)); taspeed = new jtextfield("0", 5); taspeed.sethorizontalalignment(jtextfield.center); taspeed.seteditable(false); taspeed.setopaque(false); taspeed.setfont(altitude.getfont().derivefont(25f)); altbutton = new jlabel(); altbutton.settext(ccount); canvas.add(altbutton, borderlayout.south); canvas.add(altitude, borderlayout.line_end); canvas.add(taspeed, borderlayout.line_start); //invisible jbutton btncountup = new jbutton(); btncountup.setfocusable(false); btncountup.sethideactiontext(true); // btncountup.setcontentareafilled(false); // btncountup.setborderpainted(false); canvas.add(btncountup, borderlayout.north); //invisible jbutton btncountdown = new jbutton(); btncountdown.setfocusable(false); btncountdown.sethideactiontext(true); // btncountdown.setcontentareafilled(false); // btncountdown.setborderpainted(false); canvas.add(btncountdown, borderlayout.north); // set actions buttons btncountup.setaction(countupaltitude); btncountdown.setaction(countdownaltitude); jbutton btncountups = new jbutton(); btncountups.setfocusable(false); btncountups.sethideactiontext(true); // btncountups.setcontentareafilled(false); // btncountups.setborderpainted(false); canvas.add(btncountups, borderlayout.south); //invisible jbutton btncountdowns = new jbutton(); btncountdowns.setfocusable(false); btncountdowns.sethideactiontext(true); // btncountdowns.setcontentareafilled(false); // btncountdowns.setborderpainted(false); canvas.add(btncountdowns, borderlayout.south); // set actions buttons btncountups.setaction(countuptaspeed); btncountdowns.setaction(countdowntaspeed); addkeylistener(new keyadapter() { public void keypressed(keyevent evt) { switch(evt.getkeycode()) { case keyevent.vk_left: rx1 -= 10; ry1 += 10; rx2 += 10; ry2 -= 10; x1 -=10; x2 +=10; mx1 += 10; mx2 -= 10; repaint(); break; case keyevent.vk_right: rx1 -= 10; ry1 -= 10; rx2 += 10; ry2 += 10; x1 += 10; x2 += 10; mx1 -= 10; mx2 += 10; repaint(); break; case keyevent.vk_down: y1 -= 10; y2 -= 10; ly1 -= 10; ly2 -= 10; sy1 += 10; sy2 -= 10; ry1 +=10; ry2 += 10; repaint(); break; case keyevent.vk_up: y1 += 10; y2 += 10; ly1 += 10; ly2 += 10; sy1 -= 10; sy2 += 10; ry1 -= 10; ry2 -= 10; repaint(); break; case keyevent.vk_m: system.exit(0); } } }); cp.add(canvas, borderlayout.line_start); setdefaultcloseoperation(jframe.exit_on_close); settitle("flight display"); pack(); setvisible(true); requestfocus(); } class drawcanvas extends jpanel { public void paintcomponent(graphics g) { super.paintcomponent(g); setbackground(canvas_background); g.setcolor(ground_color); //draw ground color g.drawrect(sx1 - sx1,sy1 /2, canvas_width, sy2 /2); g.fillrect(sx1 - sx1, sy1 /2, canvas_width, sy2 /2); g.setcolor(line_color); //draw line centre horizontal g.drawline(rx1, ry1 /2, rx2, ry2 /2); g.drawoval(x1 -15, y1 -15, 30, 30); g.filloval(x1 - 5, y1 -5, 10, 10); //draw line centre vertical //draw line dim g.setcolor(color.yellow); g.fillarc(300, 0, 400, 140, 0, 180); g.setcolor(line_color); g.drawline(lx1 -25, ly1 +20, lx2 +25, ly2 +20); g.drawline(lx1 -50, ly1 +40, lx2 +50, ly2 +40); g.drawline(lx1 -25, ly1 +60, lx2 +25, ly2 +60); g.drawline(lx1 -75, ly1 +80, lx2 +75, ly2 +80); g.drawline(lx1 -25, ly1 +100, lx2 +25, ly2 +100); //draw line dim g.drawline(lx1 -25, ly1 -20, lx2 +25, ly2 -20); g.drawline(lx1 -50, ly1 -40, lx2 +50, ly2 -40); g.drawline(lx1 -25, ly1 -60, lx2 +25, ly2 -60); g.drawline(lx1 -75, ly1 -80, lx2 +75, ly2 -80); g.drawline(lx1 -25, ly1 -100, lx2 +25, ly2 -100); //draw polyline centre plane g.drawpolyline(xs, ys, 8); g.setcolor(color.black); g.drawarc(300, 0, 400, 140, 0, 180); g.drawline(mx1+30, my1 + my1, mx2, my2 - my2); g.drawline(mx1-30, my1 + my1, mx2, my2 - my2); g.drawline(mx1, my1 + my1, mx2, my2 - my2); } } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { new display(); } }); } }

keyevent.vk_2 nil key number 2 in non-numpad area on standard keyboard.

public static final int vk_2 = 0x32;

the below code works fine me, not expect have not added switch case key, can little testing print ascii value of desired key , find out corresponds in keyevent class.

public void keypressed(keyevent e) { switch (e.getkeycode()) { case keyevent.vk_2: system.out.println("non numpad 2"); case keyevent.vk_numpad3: system.out.println("numpad 3"); } }

java swing jbutton jtextfield keyevent

No comments:

Post a Comment