如何设置 JLabel 文本在系统休眠前显示
How to set JLabel text to display before a system sleep
我试图在系统休眠 3,000 毫秒之前显示成功登录的文本。当我将它放在设置文本之后时它不起作用。我如何让它显示然后暂停以便有一点延迟以便用户知道他们登录?
用户正确登录后,它将继续到另一个 class 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);
假设您是从 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);
即将 GUI 操作安排到 GUI 线程
请参阅 Concurrency in Swing 了解您遇到问题的原因
有关可能的解决方案,请参阅 How to use Swing Timers
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();
我试图在系统休眠 3,000 毫秒之前显示成功登录的文本。当我将它放在设置文本之后时它不起作用。我如何让它显示然后暂停以便有一点延迟以便用户知道他们登录?
用户正确登录后,它将继续到另一个 class 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);
假设您是从 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);
即将 GUI 操作安排到 GUI 线程
请参阅 Concurrency in Swing 了解您遇到问题的原因
有关可能的解决方案,请参阅 How to use Swing Timers
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();