سؤال

This is my code:

private void jButton5MouseClicked(java.awt.event.MouseEvent evt) {                                      
    this.jButton5.setText("VietNam");
    try {
        Thread.sleep(10000);
    } catch (InterruptedException ex) {
        Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    this.jButton5.setText("Ha Noi");
}

I want the Button display "Viet Nam", and after one minute it'll change to "Ha Noi". But with my code, my Button displayed nothing and after 1 minute it displayed "Ha Noi", without "Viet Nam" appeared for a single second.

How can I fix that? I don't want to use swing.Timer because it can't pause the system completely. If there is a way to fix it, please tell me.

Thanks you guys a lot.

هل كانت مفيدة؟

المحلول

You have executed Thread.sleep() on the GUI thread, which prevented it from making any further updates of the GUI.

The most convenient way to schedule a future action in the GUI is by using javax.swing.Timer, which see in the Javadocs.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top