gpt4 book ai didi

java - 将代码延迟给定秒数

转载 作者:行者123 更新时间:2023-12-01 18:44:19 24 4
gpt4 key购买 nike

这是使用java编写的代码,延迟代码执行5秒。但它不起作用。 “this.jLabel2.setText(“TDK”);”声明不起作用。任何人都可以帮我解决这个问题吗?

 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.jLabel2.setText("TDK");
boolean result=false;
result=stop();
if(result)
{
this.jLabel1.setText("MDK");
}
}
public boolean stop()
{
String current= new java.text.SimpleDateFormat("hh:mm"
+ ":ss").format(new java.util.Date(System.currentTimeMillis()));
String future=new java.text.SimpleDateFormat("hh:mm"
+ ":ss").format(new java.util.Date(System.currentTimeMillis()+5000));

while(!(current.equals(future)))
{
current= new java.text.SimpleDateFormat("hh:mm"
+ ":ss").format(new java.util.Date(System.currentTimeMillis()));
}

return true;
}

最佳答案

您正在阻塞事件调度线程(不,也不要使用 Thread.sleep())。使用 Swing Timer :

Timer timer = new Timer(HIGHLIGHT_TIME, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jLabel1.setText("MDK");
}
});
timer.setRepeats(false);
timer.start();

其中 HIGHLIGHT_TIME 是您想要延迟设置文本的时间。

关于java - 将代码延迟给定秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18495859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com