简体   繁体   English

Thread.sleep没有编译

[英]Thread.sleep not compiling

I was trying to use Thread.sleep for something. 我试图使用Thread.sleep来做某事。 I just put it between some ImageIcon declarations like so: 我只是把它放在一些ImageIcon声明之间,如下所示:

ImageIcon cheese = new ImageIcon("C:\\ et cetera \\PictureThing.gif");
Thread.sleep(500);
ImageIcon cheese2 = new ImageIcon("C:\\ et cetera \\AnotherPictureThing.gif");

and I compiled it and ran. 我编译并运行。

Just to note, don't worry, the above code wasn't the only code, so don't tell me no duh it didn't work. 只是要注意,不要担心,上面的代码不是唯一的代码,所以不要告诉我没有,它没有工作。 What it actually did was, once I ran it in the command line, it just said something like "error: unreported exception InterruptException, must be caught or declared to be thrown". 它实际上做的是,一旦我在命令行中运行它,它只是说“错误:未报告的异常InterruptException,必须被捕获或声明被抛出”。

What am I doing wrong, and do I need to add something to it to make Thread.sleep work right? 我做错了什么,是否需要添加一些内容才能使Thread.sleep正常工作?

You need to catch the thrown InterruptedException . 你需要捕获抛出的InterruptedException Wrap your call in a try { } block try { }块中包含你的电话

try {

    Thread.sleep(500);

}
catch (InterruptedException ex) {

    ex.printStackTrace();

}

I'd really suggest reading up on try-catch-blocks and exception handling here 我真的建议在这里阅读try-catch-blocks和异常处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM