swing - How do you make a panel show multiple things one after another? (java) -


i have class extends jpanel (and it's game setup). in class, have method loops through animate/display method 8 times using loop , thread.sleep(). animate/display method first calls removeall() method, adds jlabel onto panel, revalidates , repaints. reason, when add class jframe , try run it, jframe stays blank until final run-through of loop, , final jlabel displayed. (i tested out changing number of times loop runs- no matter how many times runs, last 1 1 shows up). previously, class used extend jframe instead of jpanel, had change jpanel purposes of game. worked fine jframe- jlabels showed when supposed to. when tried write exact same code (with few slight modifications) jpanel, has problem last jlabel showing up. have idea i'm doing wrong ?

code

    import java.awt.*;     import java.util.*;     import javax.swing.*;     import java.awt.event.keyevent;      public class hardlevel extends jpanel{        public hardlevel(){       super();       rungame();       setvisible(true);       setsize(683,500);     }      public void rungame(){       (int x = 0; x < 8; x++){          animate();          try{thread.sleep(1000);}catch(interruptedexception a){}       }     }      public void animate(){         int num = 0;         num = (int)(math.random() * 13 - 1 + 1) + 1;         removeall();         jlabel desc = new jlabel("scene number "+num);         desc.setfont(desc.getfont().derivefont(font.bold, 20));         desc.setforeground(color.red);         desc.sethorizontalalignment(jlabel.left);         desc.setverticalalignment(jlabel.top);         add (desc);         revalidate();         repaint();       }    } 

above simplified version of class extends jpanel

hardlevel h = new hardlevel(); removeall(); add(h); revalidate(); repaint(); 

above code i'm using when i'm adding jframe

edit

this first time working swing in complex context, don't know edt or related that.

you can use timertask repeatedly cycle through want set data on jpanel to.

check out: http://docs.oracle.com/javase/7/docs/api/java/util/timertask.html

hard tell what's wrong implementation without seeing code.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -