繁体   English   中英

Java Nimbus L&F-按钮文本颜色不起作用

[英]Java Nimbus L&F - Button text color not working

我无法使用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
}

尝试:

UIManager.getLookAndFeelDefaults().put("Button.textForeground", Color.BLUE);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM