将JTextField的“disabledTextColor”设置为RED并禁用它将“enabled”设置为false,但它仍然显示为灰色

时间:2012-03-20 14:47:10

标签: java swing netbeans colors jtextfield

我正在使用NetBeans及其WindowDesigner。

将焦点设置为 JTextField 后,我在“属性”窗口中执行了两项操作:

  • 首先我将TextField的 disabledTextColor 字段设置为红色([0,0,51]),
  • 其次,我将TextField的启用字段设置为false。

查看预览,没有。仍然是纯灰色的。 为什么这样,我该如何改变呢?

1 个答案:

答案 0 :(得分:4)

你必须有另一个问题,这些基本的Swing方法适合我

enter image description here

import java.awt.*;
import javax.swing.*;

public class InactiveBackgroundTest {

    public JComponent makeUI() {
        JTextField s0 = new JTextField("Very long Text");
        s0.setEnabled(true);
        s0.setForeground(Color.yellow);
        s0.setBackground(Color.blue);
        //UIManager.put("FormattedTextField.inactiveBackground", Color.RED);

        JTextField s1 = new JTextField("Very long Text");
        s1.setEnabled(false);
        s1.setForeground(Color.yellow);
        s1.setBackground(Color.blue);
        s1.setDisabledTextColor(Color.yellow);

        JTextField s2 = new JTextField("Very long Text");
        s2.setEditable(false);
        s2.setForeground(Color.yellow);
        s2.setBackground(Color.blue);
        s2.setDisabledTextColor(Color.yellow);

        JPanel p = new JPanel();
        p.setBackground(Color.black);
        p.add(s0);
        p.add(s1);
        p.add(s2);
        return p;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }

    public static void createAndShowGUI() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.getContentPane().add(new InactiveBackgroundTest().makeUI());
        f.setLocationRelativeTo(null);
        f.pack();
        f.setVisible(true);
    }
}