简体   繁体   中英

How to stop thread.sleep from stopping entire JFrame

This is probably a quick and newbie fix but I am desperate at the moment. When I use a Thread.sleep, it stops the whole JFrame, is there a quick fix for this?

Sample code

public  void delay() throws InterruptedException {
    //Construct to create a delay.  
    Thread.sleep (30);
}

If its just a delay you want to implement, javax.swing.Timer is a good option. You could use it like this:

    public void delay(){
        Timer timer = new Timer(30, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                //do stuff after delay
            }
        });
        timer.start();
    }

This could probably be improved on if you showed more code. You can find a tutorial 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