问题描述
在Java中使用Nimbus L& F感觉时,我遇到了多次更改JButton背景颜色的问题。在下面的代码中,我有一个简单的Swing应用程序,它显示一个JButton并尝试每秒更改一次颜色。但是,仅应用第一种颜色。任何人都可以提供有关如何多次进行此更改的任何建议吗?我正在运行java 6.29。
While using the Nimbus L&F feel in Java, I am having problems with changing the background color for a JButton more than once. In the code below, I have a simple Swing application that displays a JButton and attempts to change the color once per second. However, only the first color is being applied. Can anyone provide any suggestions on how to make this change more than once? I'm running 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);
}
});
}
}
推荐答案
-
对于Nimbus,您必须在
for Nimbus you have to call
SwingUtilities.updateComponentTreeUI(myButton);
after any of changes for Nimbus Defaults关于
阅读
编辑:
我同意不可能以非常复杂的方式改变那种直接的方式(也许还有另一种肮脏的黑客攻击)(Nimbus L& F的开发在上半场某处结束) ),
I agreed not possible to change that direct way (maybe there is another dirty hacks) is possible that with very complicated way (development of Nimbus L&F ended somewhere on first half), another (similair) issue is in my question about Font
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); } }); } }
这篇关于在Nimbus L& F中多次更改JButton背景颜色的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!