我有这个代码,我想放一个等待..我试过的thread.sleep(),但它不好,因为它按住按钮1秒..我想我需要在这里等待()但我不知道..请帮助我,谢谢 所以我的问题是如何在actionlistener中等待?
button[1].addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {
button[1].setIcon(elemek[1]);
p+=1;
g[0]=t[1];
//here i want to wait 1 second
dosomething
}});
想法?
答案 0 :(得分:0)
如果您需要在某些代码中等待但不阻止UI,则必须在单独的线程中运行此代码,并在此帖子中使用thread.sleep()
等待
像这样的东西
button[1].addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {
Thread worker = new Thread(new Runable(){
public void run(){
button[1].setIcon(elemek[1]);
p+=1;
g[0]=t[1];
Thread.sleep(1000);
//dosomething
}});
worker.start();
}});