I have e.g.
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
defaults.put("text",Color.GREEN);`
The text is still black, but why?.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.UIManager.*;
public class test999 extends JFrame {
private JLabel jLabel1 = new JLabel();
public test999(String title) {
super(title);
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
UIDefaults defaults = new UIDefaults();
defaults.put("text",new Color(255,0,0));
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 300;
int frameHeight = 300;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
setResizable(false);
Container cp = getContentPane();
cp.setLayout(null);
jLabel1.setBounds(72, 72, 147, 57);
jLabel1.setText("text");
cp.add(jLabel1);
setVisible(true);
}
public static void main(String[] args) {
new test999("test999");
}
}
That's not how UI defaults work: "text"
is not a valid name, and no component can see your defaults
instance. Instead, try
jLabel1.setForeground(Color.red);
Also, don't use setBounds()
; use a suitable layout manager.
Addendum: As shown here, "text"
is a valid primary color key, not a component key.
I…want to…override the nimbus default.
On most L&F's you can specify the "Label.foreground"
key:
UIManager.put("Label.foreground", Color.red);
On Nimbus you have to do this:
UIManager.put("text", Color.red);
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");