我想把一些标签从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);
}
感谢您的阅读!
我看不懂英语
所以请原谅我,如果这个答案在文字上很轻
最简单的...
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);
}
}
}
有关更多详细信息,请参阅如何使用摆动计时器。
我不喜欢null
布局,我不建议null
布局,但鉴于这是学校作业,唯一可行的解决方案是使用自定义绘画,我很欣赏为什么会选择这个方向,尽管自定义绘画解决方案通常会更简单。