我在跨此 JPanel 移动此 JLabel 时遇到问题?我把代码放在下面。基本上应该发生的是,名为“guy”的 JLabel 慢慢向右移动。唯一的问题是,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){}
}
}
关于为什么在我更改变量“counterVariable”后 JLabel 只是消失而不是向右移动的任何想法。-谢谢!:)