java - Why JTextfield's text value is not changing visually by using a variable? -
i doing project make board game using java. want when player wins, score shown in jtextfield.so @ first used method jtextfield player1graphics class in declarewin method() in gameplan class.but showed me nullpointerexception.
so thought way.as can't make object of gamegui class other class because create infinite loop. have used class named player class , created variable "sname" take value "score" variable in decarewin() method.then used updatetextfied() method in player1graphics class inner class of gamegui class , set "sname" variable on it.now in runtime variable working passing value expected , jtextfield text value updating visual of jtextfield not updating.it's not changing 0 1 or 1 2.
i givin code below.............
this codes gamegui class........
public class gamegui extends jframe{ gamegui(){ super("board"); this.setvisible(true); this.setsize(700,800); this.setlocationrelativeto(null); this.setresizable(false); this.setdefaultcloseoperation(jframe.exit_on_close); this.setcontentpane(main_panel); main_panel.setlayout(new borderlayout()); main_panel.setbackground(color.white); resourcer(); } resourcer(){ //players jpanel playersroom = new jpanel(); //playersroom.setborder(borderfactory.createlineborder(color.black, 1)); playersroom.setbackground(color.white); playersroom.setlayout(new borderlayout()); jpanel player1room = new jpanel(); player1room.setlayout(new borderlayout()); //player1room.setborder(borderfactory.creatematteborder(0, 0, 0, 1, color.black)); player1room.setpreferredsize(new dimension(350,300)); player1room.add(new player1graphics(),borderlayout.center); playersroom.add(player1room,borderlayout.west); arena.add(playersroom,borderlayout.center); main_panel.add(arena,borderlayout.south); }
this player1graphics class holds jtextfield , inner class of gamegui class.
public class player1graphics extends jpanel{ private string newname; jlabel name = new jlabel("player 1",swingconstants.center); jbutton setname = new jbutton("cn"); gameplan g = new gameplan(); jbutton token = new jbutton("poison"); jtextfield showscore = new jtextfield(20); //this jtextfield player1graphics(){ //repaint(); this.setlayout(null); this.setbackground(new color(255,220,154)); //setname code & name code //jbutton setname = new jbutton("cn"); setname.setbounds(160, 40, 30, 20); setname.setborder( borderfactory.createlineborder(color.black, 1, true)); setname.settooltiptext("change name"); name.setbounds(132, 70, 80, 30); name.setopaque(true); name.setbackground(new color(102,140,185)); name.setforeground(new color(202,208,215)); name.setborder(borderfactory.createlineborder(color.black, 1)); setname.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { newname = joptionpane.showinputdialog(null,"enter name"); if (newname==null){ name.settext("player 1"); }else{ t1=1; name.settext(newname); getfirstplayername(newname); } } }); //token button code //jbutton token = new jbutton("poison"); token.setbackground(new color(233,249,254)); token.setbounds(132, 110, 80, 30); token.settooltiptext("token can used 1 time"); actionclass ac = new actionclass(); ac.getfirstplayerpoisonbutton(token); token.addactionlistener(ac); //won , showin textfieldcode jlabel won = new jlabel("won",swingconstants.center); won.setborder( borderfactory.createlineborder(new color(102,140,185), 2, true)); won.setbounds(132, 150, 50, 30); won.setbackground(new color(102,140,185)); won.setforeground(color.white); //jtextfield codes //jtextfield showwin = new jtextfield(); //gameplan gm = new gameplan(); player player = new player(); showscore.setbounds(185, 150, 30, 30); showscore.seteditable(false); //showwin.setopaque(true); //showwin.setbackground(color.white); //showwin.settext(player.sname); updatetextfield(player.sname); this.add(name); this.add(setname); this.add(token); this.add(won); this.add(showscore); repaint(); } public void paintcomponent(graphics g){ super.paintcomponent(g); graphics2d myg = (graphics2d) g; myg.setstroke(new basicstroke(1)); myg.setcolor(new color(163,193,231)); myg.filloval(25, 10, 300, 200); } public void updatetextfield(string s){ showscore.settext(s); showscore.repaint(); //joptionpane.showmessagedialog(null, showwin.gettext()); } }
this jtextfield code.................
jtextfield showscore = new jtextfield(20); player player = new player(); showscore.setbounds(185, 150, 30, 30); showscore.seteditable(false); updatetextfield(player.sname);
this updatetextfield method code..........
public void updatetextfield(string s){ showscore.settext(s); showscore.repaint(); //joptionpane.showmessagedialog(null, showscore.gettext()); }
this variable created in player class..........
public class player{ string sname = integer.tostring(gameplan.score); }
this declarewin method score variable updates , in class named gameplan class.
public class gameplan{ static int score=0; public void declarewin(){ score++; //score variable updating for(int i=0;i<20;i++){ for(int j=0;j<20;j++){ bbutton[i][j].setenabled(false); } } joptionpane.showmessagedialog(null, score); player pl = new player(); joptionpane.showmessagedialog(null, pl.sname); joptionpane.showmessagedialog(null, "you have won game"); }
i have used lot of joption see if variables passing value or not , used gettext() method see if value of jtextfield changing or not.and yes value changing visual of textfield not changing. though in inside jtextfield text value 1 or 2 shows 0 in gui.it never changes................
i have spent couple of hours solve couldn't.it's making me mad.
will please me out of mess............. :| :|
you need update textfield everytime update value of player.sname varible.
right create copy of score , save in textfield. need call updatetextfield function again new value.
see example:
public class gameplan{ static int score=0; //first new line gamegui referencetogui //get reference somethere public void declarewin(){ score++; //score variable updating //second new line referencetogui.updatetextfield(""+score); for(int i=0;i<20;i++){ for(int j=0;j<20;j++){ bbutton[i][j].setenabled(false); } } joptionpane.showmessagedialog(null, score); player pl = new player(); joptionpane.showmessagedialog(null, pl.sname); joptionpane.showmessagedialog(null, "you have won game"); }
Comments
Post a Comment