Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法之间的延迟_Java - Fatal编程技术网

Java 方法之间的延迟

Java 方法之间的延迟,java,Java,我想在巴雷西和行动之间设置延迟,但它不起作用 public void actionPerformed(ActionEvent e) {//the button action if(l<2){ if(e.getActionCommand().equals("0")){ bt[0][0].setIcon(icon[0]);// setting icon to a button s[0]=icon[0].getDescription(); x=0;

我想在巴雷西和行动之间设置延迟,但它不起作用

public void actionPerformed(ActionEvent e) {//the button action 
  if(l<2){
  if(e.getActionCommand().equals("0")){
      bt[0][0].setIcon(icon[0]);// setting icon to a button
      s[0]=icon[0].getDescription();
      x=0;
      y=0;
      l++;
  }
  if(e.getActionCommand().equals("1")){
      bt[0][1].setIcon(icon[1]);
      l++;
      s[1]=icon[1].getDescription();
      dx=0;
      dy=1;
  }

  barresi();// this method 
}

public void barresi(){
    if(l==2) {  
      flag=true;
    }
    if(flag){
      try {
        Thread.sleep(2000);
      } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
    action();
    }
}

public void action(){// i want to make delay here but when i click on a second button the image wont shown up it get null
  if(l>=2) {        
    if(s[0].equals(s[1])){
        System.out.println("OK");
        l=0;
    }
    else {
        bt[x][y].setIcon(null);
        bt[dx][dy].setIcon(null);
        l=0;
    }
  }
}
public void actionPerformed(ActionEvent e){//按钮操作
如果(l=2){
如果(s[0]。等于(s[1])){
System.out.println(“OK”);
l=0;
}
否则{
bt[x][y]。设置图标(空);
bt[dx][dy].setIcon(空);
l=0;
}
}
}
我想在
barresi
action

使用而不是
Thread.sleep
,它有时会挂起整个Swing应用程序

请看一看

只需启动Swing计时器并在
actionPerformed()
内调用
action()
方法,该方法在2秒后调用,如下面的示例代码所示

示例代码:

Timer timer = new Timer(2000, new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {            
        action();
    }
});
timer.setRepeats(false);
timer.start();
试着这样做:

public void barresi(){
    // all other stub of barresi method
    // action(); // remove it from here and move inside the Swing timer's actionPerformed method
    // start timer that will call the action method after 2 seconds
}

请留下评论,因为这是OP的第一篇帖子,并告诉OP为什么投了否决票,以使其更清楚。-1因为“不工作”并不能解释什么是错的。有例外吗?代码没有编译吗?输出是否与您期望的不同?“不工作”是什么意思?@thomashrig这没关系,但如果新OP能把它说得更清楚,那就太好了。如果您留下评论,OP可以提供更多信息。