1
使用L- & F.我不能改變文本按鈕的顏色,我使用此代碼Java的雨雲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編輯器(上述線是鄰近線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
}
這個作品,非常感謝! – 2012-08-04 18:41:39