Java UIManager只更改一次颜色(Nimbus)

Java UIManager只更改一次颜色(Nimbus),java,swing,colors,nimbus,uimanager,Java,Swing,Colors,Nimbus,Uimanager,请参阅此错误: 我在JFrame上有两个按钮。从第一个按钮开始,我通过 UIManager.put( "Button.background", new ColorUIResource(Color.red) ); SwingUtilities.updateComponentTreeUI( this.getContentPane() ); 在第二个按钮上,我用 UIManager.put( "Button.background", new ColorUIResource(Color.green)

请参阅此错误:

我在JFrame上有两个按钮。从第一个按钮开始,我通过

UIManager.put( "Button.background", new ColorUIResource(Color.red) );
SwingUtilities.updateComponentTreeUI( this.getContentPane() );
在第二个按钮上,我用

UIManager.put( "Button.background", new ColorUIResource(Color.green) );
SwingUtilities.updateComponentTreeUI( this.getContentPane() );
在这里,我使用的是ColorUIResource(如错误解决方案所述),但我的问题仍然与引用的错误中所述的相同。i、 e.UIManager在第一次单击任何按钮时更改颜色,但在后续单击时不更改颜色

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

  • 不可能直接改变这种方式

  • 重置当前的Nimbus配色方案(
    UIManager.getLookAndFeel().uninitialize();
    )可能会导致

代码

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);
            }
        });
    }
}