简体   繁体   English

刷新JPanel - 移动JLabel

[英]Refreshing a JPanel-Moving a JLabel

I am having trouble moving this JLabel across this JPanel? 我无法在这个JPanel上移动这个JLabel? I put the code below. 我把代码放在下面。 Basically what is supposed to happen, is the JLabel called "guy" slowly moves to the right. 基本上应该发生的事情是被称为“家伙”的JLabel慢慢向右移动。 The only problem is, that the JLabel isn't refreshing it just disappears after the first time I move it. 唯一的问题是,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){}
        }

    }

Any thoughts as to why the JLabel is just disappearing instead of moving to the right after I change the variable "counterVariable". 关于为什么JLabel正在消失而不是在我改变变量“counterVariable”之后向右移动的任何想法。 -Thanks! -谢谢! :) :)

Your code is causing a long-running process to run on the Swing event thread which is preventing this thread from doing its necessary actions: paint the GUI and respond to user input. 您的代码导致长时间运行的进程在Swing事件线程上运行,这阻止了该线程执行必要的操作:绘制GUI并响应用户输入。 This will effectively put your entire GUI to sleep. 这将有效地让您的整个GUI进入睡眠状态。

Issue & suggestions: 问题和建议:

  • Never call Thread.sleep(...) on the Swing Event Dispatch Thread or EDT. 永远不要在Swing Event Dispatch Thread或EDT上调用Thread.sleep(...)
  • Never have a while (true) on the EDT. 在EDT上永远不会有一段while (true)
  • Instead use a Swing Timer for all of this. 而是使用Swing Timer来完成所有这些。
  • No need to keep adding the JLabel to the JPanel. 无需继续将JLabel添加到JPanel。 Once added to the JPanel, it remains there. 一旦添加到JPanel,它仍然存在。
  • Likewise, no need to keep calling setVisible(true) on the JLabel. 同样,不需要在JLabel上继续调用setVisible(true) Once visible, it remains visible. 一旦可见,它仍然可见。
  • Call repaint() on the container holding the moving JLabel after you've moved it to request that the container and its children be re-drawn. 在移动JLabel之后,在容器上调用repaint() ,以请求重新绘制容器及其子容器。

eg, 例如,

public void drawWorld(){
  guy.setBounds(counterVariable,0,50,50);
  int timerDelay = 100;
  new javax.swing.Timer(timerDelay, new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
      countVariable++;
      guy.setBounds(counterVariable,0,50,50);
      panel.repaint();
    }
  }).start;
}

caveat: code not compiled, run, or tested in any way 警告:代码未以任何方式编译,运行或测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM