java - JTextPane not updating after values changed -
code underneath. i'm trying have display going on in jpanel of jtextpane. have button edits value of string that's supposed displayed in jtextpane. can't figure out how update jtextpane however. i've tried revalidate(), validate(), repaint(), none of seemed work.
the code complete, should able run.
import java.awt.canvas;
public class windowbuild extends jframe { /** * */ private static final long serialversionuid = 1l; private jpanel contentpane; private int health = 20; private int energy = 4; /** * launch application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { windowbuild frame = new windowbuild(); frame.setvisible(true); } }); } private class buttonhandler implements actionlistener { public void actionperformed(actionevent e) { string = e.getactioncommand(); if (which.equals("claw")){ energy = energy-1; system.out.println("player one's dragon clawed opponent. dragon's energy at: "+ energy);} else if (which.equals("wait")){ system.out.println("turn succesfully skipped");} system.out.println(getenergy()); } } public windowbuild() { buttonhandler bh; system.out.println("starting frame..."); bh = new buttonhandler(); setdefaultcloseoperation(jframe.exit_on_close); setbounds(100, 100, 800, 600); contentpane = new jpanel(); contentpane.setborder(new titledborder(null, "dragon duel", titledborder.center, titledborder.top, null, color.cyan)); setcontentpane(contentpane); contentpane.setlayout(null); jbutton btnclaw = new jbutton("claw"); btnclaw.setbounds(288, 511, 109, 39); contentpane.add(btnclaw); btnclaw.addactionlistener(bh); if (energy == 0) btnclaw.setenabled(false); jbutton btnwait = new jbutton("wait"); btnwait.setbounds(645, 511, 109, 39); contentpane.add(btnwait); btnwait.addactionlistener(bh); stringbuilder sb = new stringbuilder(); string strb = integer.tostring(health); sb.append("h: ").append(strb).append("/20"); string healthstring = sb.tostring(); jtextpane txtpnh_1 = new jtextpane(); txtpnh_1.seteditable(false); txtpnh_1.setfont(new font("impact", font.plain, 30)); txtpnh_1.settext(healthstring); txtpnh_1.setbounds(134, 511, 109, 39); contentpane.add(txtpnh_1); string strr = integer.tostring(energy); string energystring = "e: "; energystring += strr; energystring += "/4"; jtextpane txtpnh = new jtextpane(); txtpnh.seteditable(false); txtpnh.settext(energystring); txtpnh.setfont(new font("impact", font.plain, 30)); txtpnh.setbounds(39, 511, 85, 39); contentpane.add(txtpnh); } }
thanks much!!
- take time read through code conventions java programming language
- make use of appropriate layout managers, see a visual guide layout managers , using layout managers more details
- for it's worth, use
jtextfield
insteadjtextpane
, you're gaining little no benefit usingjtextpane
seem trying achieve. in fact, might better of usingjlabel
, seen don't want them editable - avoid overriding top level containers,
jframe
, instead startjpanel
, build ui on , deploy ever top level container want.
the problem have reference issue. in constructor of windowbuild
, defining ui components. means there no way can reference them anywhere else program. instead, make components need reference else instance fields.
public class windowbuild extends jframe { //...// private jtextpane txtpnh_1; private jtextpane txtpnh; //...// public windowbuild() { //...// txtpnh_1 = new jtextpane(); //...// txtpnh = new jtextpane(); //...// } private class buttonhandler implements actionlistener { public void actionperformed(actionevent e) { string = e.getactioncommand(); // can use txtpnh_1.settext , txtpnh.settext } }
Comments
Post a Comment