how to move jlabel every second?

风流意气都作罢 提交于 2019-12-01 09:57:10

问题


i try to move it to the right(x++) every seconds

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


回答1:


  • Class names begin with capital letters i.e Help
  • Swing components should be created and modified on Event Dispatch Thread
  • A new Thread is created like this:

    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:

  • How to Use Swing Timers

EDIT:

As per your questions I suggest using a Timer the creating thread point was for general knowledge.

The probelm is the Thread is not run on EDT Thread of your swing GUI where as a Timer does:

 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:

  • http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html



回答2:


Here is an Swing example of a simple puzzle game.

Java Swing Shuffle Game

When you press Pause button the title will get animate until you release the pause. Similarly you can use it for JLabel. Source code is also attached.

Hope that can help you much.




回答3:


If you put that part of the constructor in a thread, then you can call thread.sleep(1000); (1000 milliseconds for a 1 second delay) and then refresh, which should move the target across the screen.



来源:https://stackoverflow.com/questions/13226164/how-to-move-jlabel-every-second

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!