因为Swing输入控件(主要是JCombobox、JTextField、JSpinner、JTextArea等)在不可用时,默认字体颜色都是灰色,即Gray,这种颜色看不清楚,所以我想改变输入控件不可用时的字体颜色,但又不能每个控件都去手动设置,因为控件非常多,所以想用一个通用的方法,写一个通用类,在这个类中定义方法来设置控件不可用时的字体颜色,Swing控件继承这个类,这只是初步想法,各位高手有没有相关的代码,贴出来看看?非常着急,只要大家参与,都有分相赠!

解决方案 »

  1.   

    去看看javax.swing.plaf.metal.DefaultMetalTheme。
    然后只要应用这个theme,界面就使用里头的设置。具体是那个方法设置我也忘记了。你试一下。
    你可以看看那个类的源代码,继承后,覆盖其中一个方法返回的颜色。可能是下面这几个中的哪个。
    getPrimary1
    getPrimary2
    getPrimary3
    getSecondary1
    getSecondary2
    getSecondary3
      

  2.   

    yonghar(xio):这位仁兄,我把你列出来的方法都试了,没有设置不可用控件颜色的,我的代码如下:
        try {
          MetalLookAndFeel.setCurrentTheme
              (new DefaultMetalTheme() {
            
            private ColorUIResource color = new ColorUIResource(Color.BLUE);
            
            public ColorUIResource getPrimary1() {
              return color;
            }
            
            public ColorUIResource getPrimary2() {
              return color;
            }
            
            public ColorUIResource getPrimary3() {
              return color;
            }        public ColorUIResource getSecondary1() {
              return color;
            }
            
            public ColorUIResource getSecondary2() {
              return color;
            }
            
            public ColorUIResource getSecondary3() {
             return color;
           }
          });
       
          UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }再问一下:你做同样的功能吗?确实做出来了吗?
      

  3.   

    yonghar(xio):这位仁兄,同时我也试了其他方法,我自己写了一个类MyMetalTheme继承了抽象类MetalTheme,把它里面的所有方法也都试了一遍,结果也没出来。我的代码如下:
        
        //调用代码
        try {
          MetalLookAndFeel.setCurrentTheme(new MyMetalTheme());
          
          UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }//自己写的类MyMetalTheme
    public class MyMetalTheme extends MetalTheme {  private ColorUIResource color = new ColorUIResource(Color.black);
      private FontUIResource font = new FontUIResource("新宋体", Font.PLAIN, 12);
      private String name;
      
      public MyMetalTheme() {
      }  public String getName() {
        return name;
      }
      
      public FontUIResource getSubTextFont() {
        return font;
      }  public FontUIResource getWindowTitleFont() {
        return font;
      }  public FontUIResource getMenuTextFont() {
        return font;
      }  public FontUIResource getUserTextFont() {
        return font;
      }  public FontUIResource getSystemTextFont() {
        return font;
      }  public FontUIResource getControlTextFont() {
        return font;
      }  public ColorUIResource getPrimary1() {
        return color;
      }  public ColorUIResource getPrimary2() {
        return color;
      }  public ColorUIResource getPrimary3() {
        return color;
      }  public ColorUIResource getSecondary1() {
        return color;
      }  public ColorUIResource getSecondary2() {
        return color;
      }  public ColorUIResource getSecondary3() {
        return color;
      }
      
      //TestColor
      public ColorUIResource getSeparatorBackground() {
        return color;
      }}
      

  4.   

    原来印象错了 ...
    不好意思。
    看来只能用uimanager设置了。        //打印所有的颜色属性
            UIDefaults uiDefaults = UIManager.getLookAndFeelDefaults();
            Enumeration keys = uiDefaults.keys();
            while (keys.hasMoreElements()) {
                Object key = keys.nextElement();
                Object val = uiDefaults.get(key);            if (val instanceof Color) {
                    System.out.println(key.toString());
                }
            }        //设置TextField,Label,Button不可用状态的表现颜色
            uiDefaults.put("TextField.inactiveForeground", Color.BLUE);
            uiDefaults.put("Label.disabledForeground", Color.BLUE);
            uiDefaults.put("Button.disabledText", Color.BLUE);
      

  5.   

    import java.awt.*;
    import javax.swing.*;
    import java.util.*;public class Frame3 extends JFrame {
      JPanel contentPane;
      FlowLayout flowLayout = new FlowLayout();
      JTextField txtfldTest = new JTextField();
      JLabel lblTest = new JLabel();
      JComboBox combTest = new JComboBox();
      JTextArea txtareaTest = new JTextArea();  public Frame3() {
        try {
          jbInit();
        }
        catch (Exception exception) {
          exception.printStackTrace();
        }
      }  private void jbInit() throws Exception {
        contentPane = (JPanel)this.getContentPane();
        contentPane.setLayout(flowLayout);    /*这是我测试JTextField的代码,因为使用TextField.inactiveForeground没有效果,我把 TextField的所有Key均测试了一遍,还是不能实现文本框在不可用时字体为红色,真是弄不清什么原因了,怀疑是环境问题,我直接到命令行下运行,结果还是一样,真是头疼!*/
        UIManager.put("TextField.inactiveForeground", new Color(255, 0, 0));
        UIManager.put("TextField.caretForeground", new Color(255, 0, 0));
        UIManager.put("TextField.inactiveBackground", new Color(255, 0, 0));
        UIManager.put("TextField.shadow", new Color(255, 0, 0));
        UIManager.put("TextField.darkShadow", new Color(255, 0, 0));
        UIManager.put("TextField.selectionForeground", new Color(255, 0, 0));
        UIManager.put("TextField.background", Color.white);
        UIManager.put("TextField.highlight", Color.red);
        UIManager.put("TextField.light", Color.red);
        UIManager.put("TextField.selectionBackground", Color.red);
        UIManager.put("TextField.foreground", Color.red);    /*这是我测试标签和下拉框不可用时设置字体颜色为红色的代码,这两个测试没有问题*/
        UIManager.put("Label.disabledForeground", new Color(255, 0, 0));
        UIManager.put("ComboBox.disabledForeground", new Color(255, 0, 0));    /*这是测试多行文本框不可用时设置字体颜色为红色的代码,它的问题用文本框,解决不了,很是头疼!*/
        UIManager.put("TextArea.inactiveForeground", new Color(255, 0, 0));    lblTest.setText("TestLabel");
        lblTest.setEnabled(false);
        txtfldTest.setText("Testing");
        txtfldTest.setEnabled(false);
        combTest.removeAllItems();
        combTest.addItem("Test1");
        combTest.addItem("Test2");
        combTest.setSelectedIndex(0);
        combTest.setEnabled(false);
        txtareaTest.setText("TestingJTextArea");
        txtareaTest.setEnabled(false);
        contentPane.add(lblTest);
        contentPane.add(txtfldTest);
        contentPane.add(combTest);
        contentPane.add(txtareaTest);    /*我把经过上述设置后的结果均输出出来了,结果都正确,但就是颜色没有改变,太郁闷了!输出结果如下:
    TextField.inactiveForeground:java.awt.Color[r=255,g=0,b=0]
    TextField.selectionBackground:java.awt.Color[r=255,g=0,b=0]
    TextField.inactiveBackground:java.awt.Color[r=255,g=0,b=0]
    TextField.highlight:java.awt.Color[r=255,g=0,b=0]
    TextField.shadow:java.awt.Color[r=255,g=0,b=0]
    TextField.light:java.awt.Color[r=255,g=0,b=0]
    TextField.foreground:java.awt.Color[r=255,g=0,b=0]
    TextField.background:java.awt.Color[r=255,g=255,b=255]
    TextField.darkShadow:java.awt.Color[r=255,g=0,b=0]
    TextField.caretForeground:java.awt.Color[r=255,g=0,b=0]
    TextField.highlight:java.awt.Color[r=255,g=0,b=0]Label.disabledForeground:java.awt.Color[r=255,g=0,b=0]
    ComboBox.disabledForeground:java.awt.Color[r=255,g=0,b=0]TextArea.inactiveForeground:java.awt.Color[r=255,g=0,b=0]*/    UIDefaults u = UIManager.getDefaults();
        Enumeration elements = u.keys();
        for (; elements.hasMoreElements(); ) {
          Object o = elements.nextElement();
          System.out.println(o + ":" + u.get(o));
        }
      }
      public static void main(String[] args) {
        Frame3 frame3 = new Frame3();
        frame3.setSize(300, 400);
        frame3.setVisible(true);
      }
    }各位高手,我实在是找不出原因了!高手帮我看看吧,能解决我的问题,必将重谢!
      

  6.   

    你的方法没有错,不过位置错了,将
        UIManager.put("TextField.inactiveForeground", new Color(255, 0, 0));
        UIManager.put("TextArea.inactiveForeground", new Color(255, 0, 0));
    放在控件创建之前就行了  public static void main(String[] args) {
        UIManager.put("TextField.inactiveForeground", new Color(255, 0, 0));
        UIManager.put("TextArea.inactiveForeground", new Color(255, 0, 0));
        Frame3 frame3 = new Frame3();
        frame3.setSize(300, 400);
        frame3.setVisible(true);
      }
      

  7.   

    cheng_young(古道西风瘦马):
    UIManager.put("TextField.inactiveForeground", new Color(255, 0, 0));
    UIManager.put("TextArea.inactiveForeground", new Color(255, 0, 0));
    这两句话放在Frame3 frame3 = new Frame3()前面就可以,
    我觉得放在jbInit()里面也没啥区别呀?为什么呢?
      

  8.   

    JTextField txtfldTest = new JTextField();
    实际上是在构造函数的最前面执行的,也即是在jbInit之前执行的