Java Nimbus L&;F-按钮文本颜色不工作
我无法使用L&F更改文本按钮的颜色。我正在使用此代码Java Nimbus L&;F-按钮文本颜色不工作,java,button,look-and-feel,foreground,nimbus,Java,Button,Look And Feel,Foreground,Nimbus,我无法使用L&F更改文本按钮的颜色。我正在使用此代码 UIManager.getLookAndFeelDefaults().put("Button.background", Color.ORANGE); UIManager.getLookAndFeelDefaults().put("Button[Default+Pressed].textForeground", Color.BLUE); UIManager.getLookAndFeelDefaults().put("Button.for
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行)
导入java.awt.Color;
导入javax.swing.UIManager;
公共类测试扩展了javax.swing.JFrame{
/**
*创建新的表单测试
*/
公开考试(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
jButton1=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText(“测试按钮”);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(74,74,74)
.addComponent(jButton1,javax.swing.GroupLayout.DEFAULT_SIZE,240,Short.MAX_值)
.addGap(86,86,86))
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(106、106、106)
.addComponent(jButton1,javax.swing.GroupLayout.DEFAULT_SIZE,81,Short.MAX_值)
.addGap(113、113、113))
);
包装();
}//
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
/*
*设置Nimbus的外观和感觉
*/
//
/*
*如果Nimbus(在JavaSE6中引入)不可用,请使用
*默认外观。有关详细信息,请参阅
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
UIManager.getLookAndFeelDefaults().put(“Button.background”,Color.ORANGE);
UIManager.getLookAndFeelDefaults().put(“按钮[Default+按下].textForeground”,颜色为.BLUE);
UIManager.getLookAndFeelDefaults().put(“按钮.前景”,颜色.蓝色);
UIManager.getLookAndFeelDefaults().put(“按钮[Disabled].textForeground”,Color.BLUE);
UIManager.getLookAndFeelDefaults().put(“Button.disabled”,Color.BLUE);
UIManager.getLookAndFeelDefaults().put(“Button.disabledText”,Color.BLUE);
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(Test.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*
*创建并显示表单
*/
invokeLater(new Runnable()){
公开募捐{
新测试().setVisible(真);
}
});
}
//变量声明-不修改
私有javax.swing.JButton jButton1;
//变量结束声明
}
试试:
尝试:
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);