java - Program does not appear to run and does not flag any errors -
while trying compile 'guess number' program in netbeans programme seems keep on running programme not executing. when user clicks 'play' jbutton playgame method should run nothing seems happen , no exception thrown.
code below. ciaran
/* * chpter 11 gui programmong questions * 11.15 */ package chapter11gui; import java.awt.borderlayout; import java.awt.flowlayout; import java.awt.event.actionlistener; import java.awt.activeevent; import javax.swing.jbutton; import javax.swing.jpanel; import javax.swing.jtextarea; import java.awt.color; import java.awt.component; import java.awt.event.actionevent; import javax.swing.jlabel; import javax.swing.jtextfield; import javax.swing.jframe; import javax.swing.imageicon; import javax.swing.icon; import javax.swing.swingconstants; import java.util.random; public class guessgame extends jframe { //instance varibles/files private jlabel intro; private jlabel highorlow; private jbutton play; private jtextfield enteranswer; private int answer1; private int answer2; private imageicon icon1; private imageicon icon2; private jpanel panel; private random numbergenerator; private int correctanswer; //constructor willll create gui o jframe , register event handlers public guessgame() { super("guessgame"); setlayout(new borderlayout()); icon icon1 = new imageicon(getclass().getresource("bug1.gif")); icon icon2 = new imageicon(getclass().getresource("travelbug.gif")); //jtextfield user input enteranswer = new jtextfield(5); //enteranswer.addactionlistener(handler); enteranswer.settext("guess"); enteranswer.seteditable(false); //jlabel display heading intro = new jlabel("press play begin....", icon1, swingconstants.center); highorlow = new jlabel(); //place componments on panel useing flowlayout manager hold text aea panel = new jpanel(); panel.add(enteranswer); panel.add(highorlow); //jbutton play again , use anoumonus class call gamemethod play = new jbutton("play"); add(panel, borderlayout.center); add(intro, borderlayout.north); add(play, borderlayout.south); play.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { //if button clicked setart game play.settext("enjoy"); playgame(); //throw new unsupportedoperationexception("not supported yet."); //to change body of generated methods, choose tools | templates. }//end actionperfomred });//end annmous class actionlistener call addacionlistener method }//end construtor //a method play game public void playgame() { //generate randown number bewteen 1 , 100 numbergenerator = new random(); correctanswer = numbergenerator.nextint(100); panel.setbackground(color.magenta); answer1 = 0; answer2 = 0; enteranswer.seteditable(true); enteranswer.settext(""); while ((answer1 != correctanswer) && (answer2 != correctanswer)) { intro.settext("i have number bewteen 1 , 100." + "can guess number?"); enteranswer.seteditable(true); enteranswer.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { if (e.getsource() == enteranswer) { answer1 = integer.parseint(enteranswer.gettext()); } //set background red first guess panel.setbackground(color.red); //noe set wether high or 2 low if (answer1 > correctanswer) { highorlow.settext("guess high"); highorlow.seticon(icon2); } else if (answer1 < correctanswer) { highorlow.settext("you gussing low"); highorlow.seticon(icon2); }//end esle if statement }//end actionperfomred });//end actionlistener //get answer number 2 intro.settext("try again..."); enteranswer.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { if (e.getsource() == enteranswer) { answer2 = integer.parseint(enteranswer.gettext()); } //check if gettingb hot or cold //2nd answer closer first turn red if ((math.abs(answer2 - correctanswer)) > (math.abs(answer2 - correctanswer))) { panel.setbackground(color.red); } else { panel.setbackground(color.blue); } //now add hihg ot tewo low //noe set wether high or 2 low if (answer2 > correctanswer) { highorlow.settext("guess high"); highorlow.seticon(icon2); } else if (answer2 < correctanswer) { highorlow.settext("you gussing low"); highorlow.seticon(icon2); }//end esle if statement }//end actionperformed });//end actionlistener }//end while loop when answer correct //what if answer correct panel.setbackground(color.orange); enteranswer.settext("correct!!"); enteranswer.seteditable(false); play.settext("play again?"); }//end method playgame }//end class /* *chapter 11 * porgramming question 11.14 */ package chapter11gui; import javax.swing.jframe; public class guessgametest { public static void main(string[] args) { // instanistae frame form guessgame guessgame frame = new guessgame(); frame.setvisible(true); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(350, 350); }//end main }//end clas test
thanks. have removed while loop , programme running, not intended. troubleshoot using netbeans debugger, problem have cannot seem step playgame() method see going wrong. have tried using step in (f7) @ anonymous actionlistener innerclass assoc jbutton access playgame(), , have pointed cursor @ beginning of playgame , run cursor command. in both instances debugger returns test application main class. hope made sense...
have tried inserting breakpoint @ playgame() not access. playgame() method not been part of java main class?? can seem access constructor of class main class when class in question instaniated.....
your program is in fact compiling , in fact appears running well, looks you're tying swing event thread while (true)
loop.
public void playgame() { //generate randown number bewteen 1 , 100 numbergenerator = new random(); // .... etc... // ***** lock gui!! ******* while ((answer1 != correctanswer) && (answer2 != correctanswer)) { intro.settext("i have number bewteen 1 , 100." + "can guess number?"); // .... } }//end method playgame
when this, while loop continue running on swing event thread (or event dispatch thread or edt), preventing edt interacting user. cause program non-responsive.
the solution, don't this. use event-driven programming code not linear console program code project. have program change it's behaviors based on state of answer1 , answer2 fields, don't in while loop. in other words, have actionlistener's actionperformed method behave differently depending on state of these , other class fields. swing event loop game loop you'll need.
Comments
Post a Comment