Welcome 微信登录

首页 / 软件开发 / JAVA / Swing本地外观与Substance外观之间的切换问题及解决方案

Swing本地外观与Substance外观之间的切换问题及解决方案2012-01-14 blogjava 日月雨林Swing应用程序如果是在开源的Look&&Feel 之间切换,感觉很容易,但是如果把应用程序在开源外观下切换到系统默认的或者JDK自带的外观时,问题就来了。不是没有标题栏,就是标题栏的外观没有改变,用的是系统的窗口装饰。这些是因为在应用程序启动时在main方法里添加了这样一句代码造成的:

JFrame.setDefaultLookAndFeelDecorated(true);
目前解决这个问题的办法就是先将 原来的JFrame dispose掉 ,然后在new一个 JFrame ,让原来的frame 指向这个新的JFrame。不多说,看代码比较直观, 核心代码如下:

初始化应用,initComponents()方法是NetBeans IDE生成的,就不贴了。

    private static JFrame config;    private Rectangle savedBounds;    /** Creates new form Config */    public SkinChangeDemo() {        initComponents();    }    public SkinChangeDemo(boolean decor) {        setUndecorated(decor);        initComponents();    }
下面就是核心代码:

 private void saharaButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                     // TODO add your handling code here:        LookAndFeel old = UIManager.getLookAndFeel();        SubstanceSkin skin = new SaharaSkin();        if (old instanceof SubstanceLookAndFeel) {            SubstanceLookAndFeel.setSkin(skin);        } else {   //如果不是Substance的外观则切换为Substance外观                changSkin(skin);        }    }                                               private void nimbusButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                     try {            UIManager.setLookAndFeel(new NimbusLookAndFeel());            savedBounds = getBounds();            dispose();            config = null;            config = new SkinChangeDemo(false);            config.setBounds(savedBounds);            config.setVisible(true);        } catch (UnsupportedLookAndFeelException ex) {            Logger.getLogger(SkinChangeDemo.class.getName()).log(Level.SEVERE, null, ex);        }        SwingUtilities.updateComponentTreeUI(this);    }                                               private void businessButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                       LookAndFeel old = UIManager.getLookAndFeel();        SubstanceSkin skin = new BusinessSkin();        if (old instanceof SubstanceLookAndFeel) {            SubstanceLookAndFeel.setSkin(skin);        } else {    //如果不是Substance的外观则切换为Substance外观            changSkin(skin);        }    }                                                 /**     *用于将非Substance 外观的界面该为Substance外观。     * @param skin     */    private void changSkin(SubstanceSkin skin) {        savedBounds = getBounds();        dispose();        config = new SkinChangeDemo(true);        config.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);  //这句是关键代码,自己看API体会吧        config.setBounds(savedBounds);  //保持变换皮肤时位置不变        SubstanceLookAndFeel.setSkin(skin);        config.setVisible(true);        config.getRootPane().updateUI();        SwingUtilities.updateComponentTreeUI(this);    }