Java swing application not working -
here image of application:
when new button pressed question displayed , timer starts.
but after 'new' button pressed neither timer runs nor text displayed , default close operation not work. when comment out checktimer() working fine.
here code:
if(buttonevent.getactioncommand().equals("new")){ string store = buttonevent.getactioncommand(); startgame(); checktimer(); panelone.remove(buttonnew); panelone.revalidate(); panelone.repaint(); } public void startgame(){ // todo auto-generated method stub string line = null; try{ bufferedreader reader = new bufferedreader(new filereader("c:\\users\\mypc\\documents\\myfile.txt")); while((line = reader.readline()) != null){ system.out.println(line); queue.add(line); } reader.close(); } catch(ioexception exception){ exception.printstacktrace(); } // flag = true; string display = queue.remove(); textarea.settext(display); //checktimer(); } public void checktimer() { // todo auto-generated method stub int sec = 59; int min = integer.parseint(timerfield1.gettext()); while(min >= 0){ min--; if(min >= 0){ for(int = 0; < 60; i++){ try{ thread.sleep(1000); } catch(interruptedexception ie){ thread.currentthread().interrupt(); } timerfield1.settext(integer.tostring(min)); timerfield2.settext(integer.tostring(sec)); if(sec > 0){ sec--; } else{ break; } } } //textarea1.settext(integer.tostring(num)); sec = 59; } }
please guide me why not working.
thread.sleep(1000);
don't use thead.sleep when code executing on event dispatch thread (edt)
. cause gui freeze , prevent gui responding events.
when new button pressed question displayed , timer starts.
then should using swing timer
. set timer fire every second can update time. after if fires 300 times stop timer , game over.
read section swing tutorial on how use swing timers more information , working examples.
Comments
Post a Comment