java - How Could I add Jcomponent during runtime with Swing Timer -


i have problem making adding randomtext(with random size, font,x,y) jframe every seconds code

first 1 jcomponent expects randomly made string appear

public class randomtextgenerator extends jcomponent{     private string str;     private random r;     private color c;     private font f;     private int x;      private int y;     graphics g;     public randomtextgenerator(string s)     {          r = new random();         c = new color(r.nextint(255),r.nextint(255),r.nextint(255));         f = new font("random",font.bold,r.nextint(100));         x = r.nextint(100);         y = r.nextint(100)+100;         //setpreferredsize(new dimension(500,500));         str = s;     }     public void paintcomponent(graphics g)     {         g.setcolor(c);         g.setfont(f);         system.out.println(str);         g.drawstring(str, x, y);     } } 

another 1 jframe

public class textframe extends jframe{     public textframe() {         this.setsize(500, 500);         this.setdefaultcloseoperation(jframe.exit_on_close);         this.settitle("random text generator");         this.getcontentpane().setbackground(color.black);         this.setlayout(new gridlayout(1,1));         this.add(new randomtextgenerator("aaaaaaaa"));         this.add(new randomtextgenerator("bbbbbbbbbb"));         this.setvisible(true);     }     public void addrandomtext(randomtextgenerator r)     {         this.add(r);         this.add(new randomtextgenerator("ccc"));         system.out.println("method called");         this.repaint();     } } 

above code,

this.add(new randomtextgenerator("aaaaaaaa")); this.add(new randomtextgenerator("bbbbbbbbbb")); 

those ones works , display on screen problem method addrandomtext

this.add(r); this.add(new randomtextgenerator("ccc")); 

those ones not works @ all.

my main method here

public class tfmain {     public static void main(string[] args) {             scanner s = new scanner(system.in);             system.out.println("enter message");             string str = s.nextline();             textframe tf = new textframe();             timer t = new timer(1000,new actionlistener(){                  @override                 public void actionperformed(actionevent arg0) {                     tf.addrandomtext(new randomtextgenerator(str));                 }              });             t.start();         } } 

with code method called appear means timer works.

but why adding jcomponent isn't working?? adding jcomponent in addrandomtext never works(paintcomponent not called too)

please give me advice. sorry bad english


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -