我无法使用L& F更改文本按钮的颜色。我正在使用此代码
UIManager.getLookAndFeelDefaults().put("Button.background", Color.ORANGE);
UIManager.getLookAndFeelDefaults().put("Button[Default+Pressed].textForeground", Color.BLUE);
UIManager.getLookAndFeelDefaults().put("Button.foreground", Color.BLUE);
UIManager.getLookAndFeelDefaults().put("Button[Disabled].textForeground", Color.BLUE);
UIManager.getLookAndFeelDefaults().put("Button.disabled", Color.BLUE);
UIManager.getLookAndFeelDefaults().put("Button.disabledText", Color.BLUE);
背景正确为橙色,但文字颜色为黑色默认
这里是使用Netbeans Editor生成的完整代码(上面的行靠近第75行)
import java.awt.Color;
import javax.swing.UIManager;
public class Test extends javax.swing.JFrame {
/**
* Creates new form Test
*/
public Test() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("TEST BUTTON");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(74, 74, 74)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
.addGap(86, 86, 86))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(106, 106, 106)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
.addGap(113, 113, 113))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
UIManager.getLookAndFeelDefaults().put("Button.background", Color.ORANGE);
UIManager.getLookAndFeelDefaults().put("Button[Default+Pressed].textForeground", Color.BLUE);
UIManager.getLookAndFeelDefaults().put("Button.foreground", Color.BLUE);
UIManager.getLookAndFeelDefaults().put("Button[Disabled].textForeground", Color.BLUE);
UIManager.getLookAndFeelDefaults().put("Button.disabled", Color.BLUE);
UIManager.getLookAndFeelDefaults().put("Button.disabledText", Color.BLUE);
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Test().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
答案 0 :(得分:1)
尝试:
UIManager.getLookAndFeelDefaults().put("Button.textForeground", Color.BLUE);