java - JFrame components being able to call paintComponent through repaint, although using flowlayout -
i have jframe in flowlayout multiple jlabels added it, when call repaint on jlabels, paintcomponent not being called. if remove flowlayout, last jlabel added shows , repaints properly. tried use panel didn't work. i'm not sure i'm using though.
import java.awt.dimension; import java.awt.eventqueue; import java.awt.flowlayout; import java.awt.toolkit; import javax.swing.jframe; public class racingletters { public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { final jframe jframe = new jframe(); jframe.settitle("racing letters"); jframe.setdefaultcloseoperation(jframe.exit_on_close); //jframe.setextendedstate(frame.maximized_both); dimension dimension = toolkit.getdefaulttoolkit().getscreensize(); int x = (int) ((dimension.getwidth() - jframe.getwidth()) / 2); int y = (int) ((dimension.getheight() - jframe.getheight()) / 2); jframe.setlocation(x, y); jframe.setminimumsize(new dimension(500, 200)); flowlayout fl = new flowlayout(); jframe.setlayout(fl); //jframe.setlayout(null); jframe.setvisible(true); stringbuffer[] stringbufferarray = new stringbuffer[20]; char ch = 'a'; int yy = 20; (int = 0; < 5; i++) { stringbufferarray[i] = new stringbuffer(""); bufferthread bt = new bufferthread(stringbufferarray[i], ch, 10, yy); //pane.add(bt); jframe.add(bt); new thread(bt).start(); ch++; yy += 20; } } }); } }
..
import java.awt.font; import java.awt.graphics; import java.awt.graphics2d; import java.util.random; import java.util.logging.level; import java.util.logging.logger; import javax.swing.jlabel; public class bufferthread extends jlabel implements runnable { char ch; stringbuffer sb; int x,y; bufferthread(stringbuffer sb, char ch,int x, int y) { this.sb = sb; this.ch = ch; this.x = x; this.y = y; } @override public void run() { random rand = new random(); (int = 0; < 5; i++) { sb.append(ch); system.out.println(x + " " + y + " " + ch); repaint(); try { thread.sleep(rand.nextint(500)); } catch (interruptedexception ex) { logger.getlogger(bufferthread.class.getname()).log(level.severe, null, ex); } } } public void paintcomponent(graphics g) { //system.out.println(x + " " + y + " " + ch); //system.out.println("aaaa"); //stem.out.println(sb); graphics2d g2 = (graphics2d) g; font f = new font("serif", font.plain, 24); //if (sb.tostring().indexof("e") < 0) g2.drawstring(sb.tostring(), x, y); } }
the core problem jlabel
isn't providing information frame's layout manager how big be. nor telling frame's layout manager it's updated , needs resized.
why you're trying paint text on label beyond me, seen that's label design.
you should avoid using thread
when dealing swing components , should use javax.swing.timer
, swingworker
possible.
import java.awt.eventqueue; import java.awt.flowlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.timer; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class ticker { public static void main(string[] args) { new ticker(); } public ticker() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception ex) { } catch (instantiationexception ex) { } catch (illegalaccessexception ex) { } catch (unsupportedlookandfeelexception ex) { } jframe frame = new jframe("test"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new flowlayout()); frame.add(new tickerlabel()); frame.setsize(100, 100); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class tickerlabel extends jlabel { private int counter; public tickerlabel() { timer timer = new timer(500, new actionlistener() { @override public void actionperformed(actionevent e) { if (counter > 4) { ((timer)e.getsource()).stop(); } else { string text = gettext(); text += (char)(((int)'a') + counter); settext(text); } counter++; } }); timer.setrepeats(true); timer.setcoalesce(true); timer.start(); } } }
Comments
Post a Comment