简体   繁体   English

如何每秒移动jlabel?

[英]how to move jlabel every second?

i try to move it to the right(x++) every seconds 我尝试每秒钟将其移到右侧(x ++)

i try to move it with thread.. 我尝试用螺纹移动它。

  1. how to do it? 怎么做? (and can see it move every second) (并且可以看到它每秒移动一次)
  2. there are another way to do it without use thread? 还有另一种不用线程的方法吗?
  3. what layout manager that i should use? 我应该使用什么布局管理器?

heres i try.. 我在这里尝试

public class help {
    JFrame frame = new JFrame();
    JLabel label = new JLabel("target");

    public help() {
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setSize(800,600);
        frame.setLayout(new GridLayout());
        frame.add(label);
        label.setPreferredSize(new Dimension(100,100));
        label.setLocation(400, 300);

        frame.getContentPane().validate();
        frame.repaint();
        frame.setVisible(true);

        mysterious();
    }

    void mysterious(){
     ////////////////////////////////
     // part of edit responding David kroukamp  
    Thread t = new Thread(new Runnable() {
    @Override
    public void run() {
         try{

            for (int z=0; z<10; z++){
            label.setLocation((label.getLocationOnScreen().x+10), label.getLocationOnScreen().y);
            Thread.sleep(1000);  
            } 
        }catch(Exception ae){

    }
    }
});
t.start();
    //////////////////////////////



    }
    public static void main(String[]args){
        new help();  
        }
}

thanks a lot for any kind of help 非常感谢您的任何帮助

  • Class names begin with capital letters ie Help 类名以大写字母开头,即Help
  • Swing components should be created and modified on Event Dispatch Thread Swing组件应在Event Dispatch Thread上创建和修改
  • A new Thread is created like this: 这样创建一个新Thread

     Thread t = new Thread(new Runnable() { @Override public void run() { //work here } }); t.start();//start thread 

however I'd suggest a Swing Timer as it runs on EDT : 但是我建议使用Swing Timer因为它在EDT运行:

EDIT: 编辑:

As per your questions I suggest using a Timer the creating thread point was for general knowledge. 根据您的问题,我建议使用Timer来创建线程点是为了了解一般知识。

The probelm is the Thread is not run on EDT Thread of your swing GUI where as a Timer does: 该探针的问题是该线程未在您的swing GUI的EDT线程上运行,就像在Timer那样:

 int delay = 1000; //milliseconds
  ActionListener taskPerformer = new ActionListener() {
      int count=0;
      public void actionPerformed(ActionEvent evt) {
           if(count==10) {//we did the task 10 times
                 ((Timer)evt.getSource()).stop();
            }

            label.setLocation((label.getLocationOnScreen().x+10), label.getLocationOnScreen().y);
            System.out.println(SwingUtilities.isEventDispatchThread());
           count++;
      }
  };
  new Timer(delay, taskPerformer).start();

Reference: 参考:

Here is an Swing example of a simple puzzle game. 这是一个简单的益智游戏的Swing示例。

Java Swing Shuffle Game Java Swing Shuffle游戏

When you press Pause button the title will get animate until you release the pause. 当您按下“ Pause按钮时,标题将变为动画,直到您释放暂停。 Similarly you can use it for JLabel . 同样,您可以将其用于JLabel Source code is also attached. 还附带了源代码。

Hope that can help you much. 希望对您有所帮助。

If you put that part of the constructor in a thread, then you can call thread.sleep(1000); 如果将构造函数的该部分放在线程中,则可以调用thread.sleep(1000); (1000 milliseconds for a 1 second delay) and then refresh, which should move the target across the screen. (1000毫秒(1秒钟的延迟)),然后刷新,这将使目标在屏幕上移动。

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

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