如何在不阻塞线程的情况下延迟回复?

huangapple go评论84阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年7月26日 18:55:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63099159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定