java - How to update jlabel while running animation? -
i have little animation rectangle, jlabel blue background, moving down screen. running thread this.
now, want have jlabel shows current position of rectangle.
public void run() { pnl.requestfocus(); x = (int) p.getx(); //find location of rectangle y = (int) p.gety(); while (y < 450) { rectanglelabel.setlocation(x, y); //reset location of rectangle y += 10; try { thread.sleep(100); } catch (interruptedexception e) { } } }
now want insert code inside while statement.
locationlabel.settext(string.valueof(450-y));
but every time do, jlable updates rectangle doesnt move anymore.
how go about?
try :
run example
import javax.swing.*; import java.lang.*; class testthread extends jframe implements runnable{ jlabel rectanglelabel,locationlabel; int x,y=0; testthread() { setvisible(true); setlayout(null); rectanglelabel=new jlabel("rectangle"); rectanglelabel.setbounds(10,10,100,100); locationlabel=new jlabel(); locationlabel.setbounds(200,200,100,100); add(rectanglelabel); add(locationlabel); setsize(1000,1000); thread t=new thread(this); t.run(); } public void run() { while (y < 450) { rectanglelabel.setlocation(x, y); //reset location of rectangle y += 10; locationlabel.settext(""+y); try { thread.sleep(100); } catch (interruptedexception e) { } if(y==440) { y=0; } } } public static void main(string args[]) { testthread t=new testthread(); } }
Comments
Post a Comment