Swing 中换皮肤 解决Component creation must be done on Event Dispatch Thread

用java swing 给页面设置皮肤样式的时候出现了这个错误:

org.jvnet.substance.api.UiThreadingViolationException: Component creation must be done on Event Dispatch Thread


后来查了很多资料,发现是由于swing 的线程不安全引起的,

解决方案如下:

在main方法里,程序的主入口加上:

 SwingUtilities.invokeLater(new Runnable() {  
                public void run() {  
                    
                    new ClientLoginFrame().setVisible(true); //这个就是程序界面初始化
                }  
            });  

需要换皮肤的窗体必须是线程安全情况下新建

但是由于使用netbeans IDE ,生成的是FrameView

package receiveemail;

import javax.swing.SwingUtilities;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class ReceiveEmailApp extends SingleFrameApplication {

    @Override protected void startup(){ 
        show(new ReceiveEmailView(this));
    }
    @Override protected void configureWindow(java.awt.Window root) {
    }
    public static ReceiveEmailApp getApplication() {
        return Application.getInstance(ReceiveEmailApp.class);
    }
    public static void main(String[] args) {
       launch(ReceiveEmailApp.class, args);
    }
}

可以如下修改

 @Override protected void startup(){ 
         final ReceiveEmailApp app2=this;
        //show(new ReceiveEmailView(this));
         SwingUtilities.invokeLater(new Runnable() {  
                private ReceiveEmailApp app1=app2;
                public void run() {  
                    
                    show(new ReceiveEmailView(app1));//这个就是程序界面初始化
                }  
         });  
    }

你可能感兴趣的:(java,thread,swing,String,Class,Netbeans)