java frame睡眠,如何在Java Swing应用程序中暂停/睡眠/等待?

在Java Swing应用中,使用JLabel动画时遇到延迟问题?本文教你如何通过Timer类每1000毫秒执行一次updateLabels方法,确保动画平滑过渡,避免程序冻结。不再错过关键帧,提升用户体验。
摘要由CSDN通过智能技术生成

I'm creating an animation using 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].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++;

}

and I have a button that updates the labels

btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

while (currentIndexLabel != paint.length-1) {

updateLabels();

}

}

});

But, I don't know how to wait, say for example 1000ms, until next change. When I add this:

try { Thread.sleep(1000); } catch (Exception e){}

into my ActionListener:

btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

while (currentIndexLabel!=paint.length-1) {

updateLabels();

try { Thread.sleep(1000); } catch (Exception e){}

}

}

});

its not working. It just stopping for a while and I don't see changes between the first and last frames. Is it possibile to Wait 1000ms without stopping the program? When I delete the while loop and try section, and click my button it is changing pretty well...

How can I do this?

解决方案

Using Thread#sleep method in swing applications in main thread will cause the GUI to freeze (since the thread sleeps, events cannot take place). Thread#sleep method in swing applications is only allowed to be used only by SwingWorkers, and this in their #doInBackround method.

In order to wait in a swing application (or do something periodically), you will have to use a Swing Timer. Take a look at an example i have made:

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));

}

}

Output after "Start" button is pressed:

Current Time is: Mon Feb 25 13:30:44 EET 2019

Current Time is: Mon Feb 25 13:30:45 EET 2019

Current Time is: Mon Feb 25 13:30:46 EET 2019

As you can see, Timer's action listener fires every second.

So in your case:

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());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值