简体   繁体   中英

Thread.sleep not compiling

I was trying to use Thread.sleep for something. I just put it between some ImageIcon declarations like so:

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".

What am I doing wrong, and do I need to add something to it to make Thread.sleep work right?

You need to catch the thrown InterruptedException . Wrap your call in a try { } block

try {

    Thread.sleep(500);

}
catch (InterruptedException ex) {

    ex.printStackTrace();

}

I'd really suggest reading up on try-catch-blocks and exception handling here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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