java - Why do I have to use setvisible() on my JFrame when I change components? -
so sake of simplicity set little test code figure out problem. have jframe
, added 'this' (i extended main class jcomponent
save time). component fills in red background. have sleep 2 seconds , type this.
f.remove(this); thing t = new thing(); f.add(t); f.setvisible(true);
f being jframe
object , 'thing' class extending jcomponent
paints blue background..
when comment out setvisible()
no longer changes blue.. i've tried using t.setvisible(true) seems have make frame visible again, not component
does know why have call that... or if there way change components within single frame?
"basically have jframe , added 'this' (i extended main class jcomponent save time). component fills in red background. have sleep 2 seconds , type this."
don't "sleep" program. instead use
java.swing.timer
perform repeated tasks on gui or animation. see more @ how use swing timers. can see bunch oftimer
examples here , here , here , here , hereinstead of trying add , remove panels using
cardlayout
allows switch between views. avoid lot of problems come with adding , removingcomponents/containers
. see more @ how use cardlayout. see simple example here.to answer main question, whenever remove , add components frame, need
revalidate()
it.setvisible()
takes care of you.
side note
- seems lot adding removing background panels) change background. why not
setbackround()
? can switch colors use oftimer
Comments
Post a Comment