如何在java swing应用程序中暂停/睡眠/等待?
我正在使用JLabel创建动画如何在java swing应用程序中暂停/睡眠/等待?,java,swing,Java,Swing,我正在使用JLabel创建动画 public void updateLabels() { label.setIcon(new ImageIcon(new Paint().getScaledImage(paint[currentIndexLabel].imageCALA,300,300))); label_1.setIcon(new ImageIcon(new Paint().getScaledImage(paint[currentIndexLabel].imageStat
public void updateLabels() {
label.setIcon(new ImageIcon(new Paint().getScaledImage(paint[currentIndexLabel].imageCALA,300,300)));
label_1.setIcon(new ImageIcon(new Paint().getScaledImage(paint[currentIndexLabel].imageStates,300,300)));
label_2.setIcon(new ImageIcon(new Paint().getScaledImage(paint[currentIndexLabel].imageStrategies,300,300)));
label_3.setIcon(new ImageIcon(new Paint().getScaledImage(paint[currentIndexLabel].imagekD,300,300)));
currentIndexLabel++;
}
我有一个更新标签的按钮
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
while (currentIndexLabel != paint.length-1) {
updateLabels();
}
}
});
但是,我不知道如何等待,比如说1000毫秒,直到下一次改变。当我添加以下内容时:
try { Thread.sleep(1000); } catch (Exception e){}
进入我的ActionListener:
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
while (currentIndexLabel!=paint.length-1) {
updateLabels();
try { Thread.sleep(1000); } catch (Exception e){}
}
}
});
它不起作用。它只是停了一会儿,我看不到第一帧和最后一帧之间的变化。是否可以在不停止程序的情况下等待1000毫秒?当我删除while循环并尝试部分,然后单击我的按钮时,它的变化非常好
如何做到这一点?在main线程中的swing应用程序中使用Thread#sleep
方法将导致GUI冻结(因为线程休眠,事件无法发生)#doInBackround
方法中
为了在swing应用程序中等待(或定期执行某些操作),您必须查看我制作的一个示例:
import java.awt.FlowLayout;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer; //Note the import
public class TimerExample extends JFrame {
private static final int TIMER_DELAY = 1000;
private Timer timer;
public TimerExample () {
super();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(200, 200);
setLocationRelativeTo(null);
getContentPane().setLayout(new FlowLayout());
timer = new Timer(TIMER_DELAY, e -> {
System.out.println("Current Time is: " + new Date(System.currentTimeMillis()));
});
//timer.setRepeats(false); //Do it once, or repeat it?
JButton button = new JButton("Start");
button.addActionListener(e -> timer.start());
getContentPane().add(button);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new TimerExample().setVisible(true));
}
}
按下“启动”按钮后的输出:
当前时间为:2019年2月25日星期一13:30:44 EET
当前时间为:2019年2月25日星期一13:30:45 EET
当前时间为:2019年2月25日星期一13:30:46 EET
如您所见,计时器的动作侦听器每秒都会触发
所以在您的情况下:
timer = new Timer(TIMER_DELAY, e -> {
if (currentIndexLabel != paint.length-1) {
upateLabels();
timer.restart(); //Do this check again after 1000ms
}
});
button.addActionListener(e -> timer.start());
试试看这个-。与使用四个标签相比,直接加载动画gif似乎更为合理,但如何在我的代码中使用它,我看到您替换了ActionListener的事件,当我尝试使类似的事情不起作用时:Dhmm我在这里有错误,我的编译器在您提交的整个文本下面划线<代码>此行有多个标记-行断点:MyCanvasInWindow[line:95]-MyCanvasInWindow(Paint[])-构造函数计时器(int,(e)->{})未定义@Potato Hmmm…您使用的java版本是什么?@Potato还注意到计时器的包了吗<代码>导入javax.swing.Timer代码>哈哈,我注意到我的导入中有不同的计时器,但这一行的箭头
多个标记仍然有错误-语法错误,插入“…VariableDeclaratorId”以完成FormalParameterList-标记上的语法错误“->”,@expected-语法错误,插入“[]”以完成维度