我無法越過此JPanel移動此JLabel?我把下面的代碼。基本上應該發生的是,JLabel所謂的「傢伙」慢慢地向右移動。唯一的問題是,JLabel不會刷新它,在我第一次移動它之後就會消失。刷新JPanel中移動的JLabel
public class Window extends JFrame{
JPanel panel = new JPanel();
JLabel guy = new JLabel(new ImageIcon("guy.gif"));
int counterVariable = 1;
//Just the constructor that is called once to set up a frame.
Window(){
super("ThisIsAWindow");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(panel);
panel.setLayout(null);
}
//This method is called once and has a while loop to exectue what is inside.
//This is also where "counterVariable" starts at zero, then gradually
//goes up. The variable that goes up is suposed to move the JLabel "guy"...
public void drawWorld(){
while(true){
guy.setBounds(counterVariable,0,50,50);
panel.add(guy);
counterVarialbe++;
setVisible(true);
try{Thread.sleep(100)}catch(Exception e){}
}
}
任何想法,爲什麼將JLabel剛剛消失,而不是移動到右側後,我改變變量「counterVariable」。 - 謝謝! :)
你從監聽器裏或在組件的調用從事件指派線程(例如,這種方法繪製方法或...)? – Untitled
在我去的主要方法中,Window gui = new Window();然後> gui.drawWorld(); yup ... –