英文:
How to delay an answer while not freezing the thread?
问题
Simple question.
Thread.sleep(x) freezes the entire code so even Buttons stay the way they are (pressed unpressed whatever)
I want to basicially click a button, "wait" for the computer to do its thing for x amount of time and then output something.
public class bsp extends JFrame {
DrawPanel drawPanel = new DrawPanel();
public bsp() {
setSize(600,600);
JButton Hit = new JButton("Hit him");
Hit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(1500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("I hit you back!");
}
}).start();
}
});
Hit.setSize(80, 30);
Hit.setLocation(200, 400);
add(Hit);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(drawPanel);
setVisible(true);
}
private static class DrawPanel extends JPanel {
protected void paintComponent(Graphics g) {
// Drawing code here
}
}
public static void main(String[] args) {
new bsp();
}
}
As you can see, the button "stays" pressed and the whole program is frozen.
But I basically want to simulate the "A.I." thinking before answering, without freezing everything.
英文:
Simple question.
Thread.sleep(x) freezes the entire code so even Buttons stay the way they are (pressed unpressed whatever)
I want to basicially click a button, "wait" for the computer to do it's thing for x amount of time and then output something.
public class bsp extends JFrame {
DrawPanel drawPanel = new DrawPanel();
public bsp() {
setSize(600,600);
JButton Hit = new JButton("Hit him");
Hit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Thread.sleep(1500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("I hit you back!");
}
});
Hit.setSize(80, 30);
Hit.setLocation(200, 400);
add(Hit);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(drawPanel);
setVisible(true);
}
private static class DrawPanel extends JPanel {
protected void paintComponent(Graphics g) {
}
}
public static void main(String[] args) {
new bsp();
}
}
As you can see, the button "stays" pressed and the whole program is frozen.
But I basicially want to simulate the "A.I." thinking before answering, without freezing everything.
答案1
得分: 0
考虑使用Timer
来防止主线程冻结:
import javax.swing.*;
import java.awt.event.*;
ActionListener task = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// 在这里编写在延迟时间后执行的代码
}
};
Timer countdown = new Timer(1000, task);
countdown.setRepeats(false);
countdown.start();
其中 1000 是延迟时间,以毫秒为单位,在actionPerformed
函数内部是您设置的延迟时间后代码执行的位置。
英文:
Consider using a Timer
to prevent main thread from freezing :
import javax.swing.*;
import java.awt.event.*;
ActionListener task = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
}
};
Timer countdown = new Timer(1000 ,task);
countdown.setRepeats(false);
countdown.start();
where 1000 is the delay time in milliseconds and inside the actionPerformed
function is where your code executes after the delay time set.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论