开发者

hiding button, then waiting 2 seconds before showing it again, after clicking the button

I have this school project I'm making, where I need to make my code hide a button, and then sleep, and then continue, but it never hides the button.

I have one class which is started by a button press, it then starts another method which should hide the button, and then it starts a method in another class which does a bunch of things which it does perfectly, and then it starts a 3rd method in a 3rd class, where the first thing it should do is sleep, but it sleeps before hiding my button, and totally skips hiding my button.

Here is the codes part (don't mind the danish value/method names):

public void turncard(final int navn,boolean spiller){
    knap.setVisible(false);
    EqualCheck.storevalue(this,spiller);
}

i know this part is surrounded by a if.. but it does get called like this.

if(spiller){                   //tester om det er spilleren der har vendt kort,
   kort.repaint();             //og hvis det er, så starter den computerens
   Main.spillet.computertur(); //tur.
}

and then at the start of the "computertur" method

public void computertur() {
    for(int i = 0; i < kortene.size(); i++) {
        kortene.get(i).knap.setEnabled(false);
    }
    try {
        Thread.sleep(2000);
    } catch ( Exception e) {
        System.out.println("sleep failed.");
    }

I did read about the invokeAndWait command which should be better for EDT:s, but how can I use it to pause mythread... or even, a better question, why doesn't it hide the button? :) I mean, in my head calling the setVisi开发者_运维知识库ble(false) before anything else will hide it before it does anything else.. Why doesn't it?


You should never invoke Thread.sleep(...) while executing code on the Event Dispatch Thread (EDT). This will freeze the GUI and prevent it from repainting iteself.

In general your code needs to execute on a separate Thread, except for the code that sets the button invisble. Read the section from the Swing tutorial on Concurrency for more information and solutions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜