如何延迟动画移动jlabel java swing?

时间:2017-05-09 10:53:51

标签: java swing

我想移动一些从x到y的标签!但我不知道如何推迟它!

我曾经尝试过Thread.sleep,但它没有用,有人说这是因为阻止了EDT,但由于我的英语不好,我无法在互联网上学习自己!请帮帮我一个例子!谢谢!

这是我的代码:

    public void jump(JLabel x, int y){
         x.setLocation(x.getX()+y, 310);
 }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        jump(frog3,100);
        //i want to delay each jump about 1 second! please give me some example! 
        jump(fr0g3,-200);

        jump(fr0g2,-100);

        jump(frog3,200);

        jump(frog2,200);

        jump(frog1,100);        

        jump(fr0g3,-200);

        jump(fr0g2,-200);

        jump(fr0g1,-200);

        jump(frog3,100);

        jump(frog2,200);

        jump(frog1,200);

        jump(fr0g2,-100);

        jump(fr0g1,-200);

        jump(frog1,100);

    }                                        

感谢阅读!

1 个答案:

答案 0 :(得分:3)

  

我读不懂英文

如果这个答案对文字很清楚,请原谅我

最简单的......

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label = new JLabel("Testing");
        private int delta = 4;

        public TestPane() {
            setLayout(null);
            label.setSize(label.getPreferredSize());
            add(label);
            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int x = label.getX() + delta;
                    if (x + label.getWidth() > getWidth()) {
                        x = getWidth() - label.getWidth();
                        delta *= -1;
                    } else if (x < 0) {
                        x = 0;
                        delta *= -1;
                    }
                    int y = (getHeight() - label.getHeight()) / 2;
                    label.setLocation(x, y);
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }
}

有关详细信息,请参阅How to use Swing Timers

我不喜欢null版面,我不推荐null版面,但我们认为这是一项学校作业,唯一可行的解​​决方案是使用自定义绘画,我很欣赏为什么可以选择这个方向,虽然定制的绘画解决方案通常会更简单。