2

在 Java 中使用 Nimbus L&F 感觉时,我在多次更改 JButton 的背景颜色时遇到了问题。在下面的代码中,我有一个简单的 Swing 应用程序,它显示一个 JButton 并尝试每秒更改一次颜色。但是,仅应用了第一种颜色。任何人都可以就如何多次进行此更改提供任何建议吗?我正在运行 java 6.29。

public class NimbusTest3 extends JFrame {
    private javax.swing.JButton button;

    public NimbusTest3(){
        button = new javax.swing.JButton();
        button.setText("Text");
        this.add(button, BorderLayout.CENTER);
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        this.pack();

        Timer t = new Timer(1000, new ActionListener() {
            Random r = new Random();
            @Override
            public void actionPerformed(ActionEvent e) {
                UIDefaults buttonDefaults = UIManager.getLookAndFeelDefaults();
                Color c = new Color(r.nextInt(
                        256), r.nextInt(256), r.nextInt(256));
                System.out.println(c);
                buttonDefaults.put("Button.background", c);
                button.putClientProperty("Nimbus.Overrides", buttonDefaults);
                button.putClientProperty("Nimbus.Overrides.InheritDefaults", false);
                SwingUtilities.updateComponentTreeUI(button);
                button.repaint();
            }
        });
        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 NimbusTest3().setVisible(true);
            }
        });
    }
}
4

2 回答 2

4

编辑 :

我同意不可能改变那种直接的方式(也许还有另一个肮脏的黑客)有可能以非常复杂的方式(Nimbus L&F 的开发在上半年的某个地方结束),另一个(类似的)问题是我关于字体的问题

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 NimbusTest3 extends JFrame {

    private static final long serialVersionUID = 1L;
    private javax.swing.JButton button;

    public NimbusTest3() {
        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 NimbusTest3().setVisible(true);
            }
        });
    }
}
于 2012-05-21T14:11:50.720 回答
1

您需要添加这一行:

button.setBackground(c);

到你的代码。其他代码 - buttonDefaults, putClientProperty, updateComponentTreeUI,repaint都不是必需的。

于 2012-05-21T14:11:53.557 回答