我想用Numberfields和一个按钮制作一个Java程序。
当您单击该按钮时,它会等待并等待在Numberfield中输入的时间。Integers可以正常工作,但该功能不起作用。

这是代码:

public void jButton1_ActionPerformed(ActionEvent evt) {
    sekunden = Integer.parseInt(jNumberField3.getText());
    minuten = Integer.parseInt(jNumberField2.getText());
    stunden = Integer.parseInt(jNumberField1.getText());
    zeit = sekunden + minuten*60 + stunden*60*60;//berechnet die zeit in sekunden
    TimeUnit.SECONDS.sleep(zeit);//here is the error
    System.out.println("zeit");
} // end of jButton1_ActionPerformed


错误消息如下所示:

ES.java:78:27: error: unreported exception InterruptedException; must be caught or declared to be thrown TimeUnit.SECONDS.sleep(zeit);

最佳答案

方法调用TimeUnit.SECONDS.sleep(zeit)引发必须捕获的InterruptedException

public void jButton1_ActionPerformed(ActionEvent evt) {
   try {
    sekunden = Integer.parseInt(jNumberField3.getText());
    minuten = Integer.parseInt(jNumberField2.getText());
    stunden = Integer.parseInt(jNumberField1.getText());
    zeit = sekunden + minuten*60 + stunden*60*60;//berechnet die zeit in sekunden
    TimeUnit.SECONDS.sleep(zeit);//here is the error
    System.out.println("zeit");
    }catch (InterruptedException e){
      //handle the exception
   }
}

10-02 06:40