【问题标题】:UIManager Changes color only once (Nimbus)UIManager 只改变一次颜色 (Nimbus)
【发布时间】:2012-12-18 18:45:56
【问题描述】:

参考这个错误:

4848910 : UIManager only updates colors once

我在 JFrame 上有两个按钮。从第一个按钮开始,我将 LAF 颜色更改为

UIManager.put( "Button.background", new ColorUIResource(Color.red) );
SwingUtilities.updateComponentTreeUI( this.getContentPane() );

在第二个按钮上,我正在使用

更改 LAF 颜色
UIManager.put( "Button.background", new ColorUIResource(Color.green) );
SwingUtilities.updateComponentTreeUI( this.getContentPane() );

这里我使用的是 ColorUIResource(如 bug 的解决方案所述),但我的问题与引用的 bug 中描述的相同。即 UIManager 在第一次单击任何按钮时更改颜色,但在后续单击时不会更改颜色。

我错过了什么吗?任何帮助将不胜感激。

【问题讨论】:

    标签: java swing colors nimbus uimanager


    【解决方案1】:

    代码

    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import javax.swing.JFrame;
    import javax.swing.LookAndFeel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    import javax.swing.UIDefaults;
    import javax.swing.UIManager;
    import javax.swing.UIManager.LookAndFeelInfo;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class NimbusTestButtonsBackground extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private javax.swing.JButton button;
    
        public NimbusTestButtonsBackground() {
            button = new javax.swing.JButton();
            button.setText("Text");
            add(button);
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            this.pack();
            Timer t = new Timer(1000, new ActionListener() {
    
                private Random r = new Random();
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    Color c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
                    try {
                        LookAndFeel lnf = UIManager.getLookAndFeel().getClass().newInstance();
                        UIDefaults uiDefaults = lnf.getDefaults();
                        uiDefaults.put("nimbusBase", c);
                        UIManager.getLookAndFeel().uninitialize();
                        UIManager.setLookAndFeel(lnf);
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
                    UIDefaults defaults = UIManager.getDefaults();
                    defaults.put("Button.background", c);
                    SwingUtilities.updateComponentTreeUI(button);
                }
            });
            t.start();
        }
    
        public static void main(String args[]) {
            try {
                for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (Exception e) {
                return;
            }
    
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new NimbusTestButtonsBackground().setVisible(true);
                }
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-13
      • 2021-02-05
      • 1970-01-01
      • 2011-11-02
      • 2014-08-02
      • 1970-01-01
      • 2016-09-17
      • 2015-03-07
      相关资源
      最近更新 更多