use*_*339 0 java events swing jpanel
我正在创建一个swing应用程序.它包括使用一些耗时的代码调用函数.
问题是"耗时的代码",它在Label文本设置之前调用.我希望标签在进入下一行之前设置.这为什么会出现?
myFunction()
{
myLabel.setText("Started");
//time consuming code which creates object of another class
}
Run Code Online (Sandbox Code Playgroud)
注意:启动整个应用程序时,我确实使用了java.awt.EventQueue.invokeLater
您应该在一个单独的线程中运行耗时的代码:
myFunction(){
myLabel.setText("Started");
new Thread(new Runnable(){
@Override
public void run() {
//time consuming code which creates object of another class
}
}).start();
}
Run Code Online (Sandbox Code Playgroud)