0

このバグを参照してください:

4848910 : UIManager は一度だけ色を更新します

JFrame に 2 つのボタンがあります。最初のボタンから LAF Color を変更しています

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

2番目のボタンで、LAFの色を変更しています

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

ここでは ColorUIResource を使用しています (バグの解決で説明されているように) が、私の問題は参照されたバグで説明されているものと同じままです。つまり、UIManager は、いずれかのボタンが初めてクリックされたときに色を変更しますが、その後のクリックでは色を変更しません。

何か不足していますか?どんな助けでも大歓迎です。

4

1 に答える 1

2

コード

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);
            }
        });
    }
}
于 2013-01-04T10:57:09.760 に答える