【问题标题】:Set an JTextField's »disabledTextColor« to RED and disabled it setting »enabled« to false, but it still shows up GRAY将 JTextField 的 »disabledTextColor« 设置为 RED 并禁用它,将 »enabled« 设置为 false,但它仍然显示为 GREY
【发布时间】:2012-04-05 01:13:25
【问题描述】:

我正在使用 NetBeans 及其 WindowDesigner。

将焦点设置到 JTextField 我在“属性”窗口中做了两件事:

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

查看预览,什么都没有。还是纯灰色。 为什么会这样?我该如何更改?

【问题讨论】:

    标签: java swing netbeans colors jtextfield


    【解决方案1】:

    你一定有另一个问题,这些基本的 Swing 方法对我有用

    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);
        }
    }
    

    【讨论】:

    • @B.不,永远不要,请我不想受到框架实现的方法的限制,顺便说一句,WindowBuilder 也可以访问大多数方法,两种方式1) 正确覆盖弹出属性中的正确方法,@ 987654325@ 在 CodeEditor 中编辑代码
    • @mKorbel 是正确的。也可以编辑 GUI 设计器的属性以生成所需的生成代码,但您必须知道要编辑什么prori。有一个例子here。打个比方,学木工然后学电锯。
    猜你喜欢
    • 2013-08-14
    • 1970-01-01
    • 2012-05-14
    • 2022-12-15
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多