简体   繁体   English

如何设置JLabel文本在系统休眠之前显示

[英]How to set JLabel text to display before a system sleep

I'm trying to display the text of Successful Login before a system sleep for 3,000 miliseconds. 我试图在系统睡眠3,000毫秒之前显示成功登录的文本。 Its not working when I place it right after the set text. 当我将其放置在设置的文本之后时,它不起作用。 How do I get it to display then pause so there is a bit of delay so the user knows that they loging in? 我如何使其显示然后暂停,所以会有一些延迟,以便用户知道他们已登录?

After the user correctly logs-in it will continue to a different class where the JFrame will close 用户正确登录后,它将继续到其他类,其中JFrame将关闭

l_Message.setForeground(Color.green);
l_Message.setText("Succesful Login");

try{
    Thread.sleep(3000);
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}

PLOGIN post_login = new PLOGIN();
post_login.postlogin_UI(login_JFrame);

See Concurrency in Swing for the reason why you're having problems 有关遇到问题的原因,请参见Swing中的并发。

See How to use Swing Timers for a possible solution 请参阅如何使用Swing计时器以获取可能的解决方案

import javax.swing.Timer

//...

l_Message.setForeground(Color.green);
l_Message.setText("Succesful Login");
Timer timer = new Timer(3000, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        PLOGIN post_login = new PLOGIN();
        post_login.postlogin_UI(login_JFrame);
    }
});
timer.start();

Assuming that you are calling this from outside of the GUI thread(which I believe that you should be), you could try the following: 假设您是从GUI线程之外调用此程序(我认为应该如此),则可以尝试以下操作:

    EventQueue.invokeLater(() -> {
        l_Message.setForeground(Color.green);
        l_Message.setText("Succesful Login");
    });

    try{
        Thread.sleep(3000);
    } catch(InterruptedException ex) {
        Thread.currentThread().interrupt();
    }

    PLOGIN post_login = new PLOGIN();
    post_login.postlogin_UI(login_JFrame);

ie schedule GUI operations to the GUI thread 即安排GUI操作到GUI线程

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

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