java - Smooth components automatic refresh after modifications or loops (ex: timer) -
i've looked many place , can't find out how refresh automatically frame. work (since i've used many examples there) when click buttons, not automatically.
i want create minesweeper game, , adding timer in it, works except timer counts, updates when button clicked. therefore, redoing layout won't work...
tough know timer works, if put system.out in it, see @ stable second rate.
i put below little program, not game since code long, little 1 built same circumstances, fixing fix problem overall.
in case, don't have developed button other program, time refresh when i'm messing window.
import java.awt.borderlayout; import java.awt.component; import java.awt.gridlayout; import java.util.timer; import java.util.timertask; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtextfield; public class clocktest { jframe frame = new jframe(); jbutton test1 = new jbutton(); jtextfield timer1 = new jtextfield(); int hour = 0, min = 0, sec = 0; string timing; public clocktest() { frame.setlayout(new borderlayout()); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(center(), borderlayout.center); frame.pack(); frame.setvisible(true); } public component center() { jpanel pan = new jpanel(); pan.setlayout(new gridlayout(1,2)); test1.settext("dummy"); timer1.seteditable(false); timer1.settext(timing); pan.add(test1); pan.add(timer1); return pan; } public void count() { sec++; if(sec == 60) { sec -= 60; min++; } if(min == 60) { min -= 60; hour++; } timing = hour + ":" + min + " : " + sec; //this in game refresh, set flags, mine "exploded"... //but since i'm not pressing anything... not happen. frame.add(center(), borderlayout.center); } public static void main(string[]args) { final clocktest test1 = new clocktest(); timertask task = new timertask() { public void run() { test1.count(); } }; timer t = new timer(); t.schedule(task,0,1000); } }
jpanel in puzzle game not updating
after changing components, need 'refresh' swing component calling invalidate() or revalidate()
Comments
Post a Comment