【问题标题】:Change JTextField enabled background color更改启用 JTextField 的背景颜色
【发布时间】:2014-09-12 20:12:35
【问题描述】:

我有一个关于JTextField 背景颜色的问题。如何在启用的文本字段中更改它(编辑时)? setBackground 仅适用于禁用的文本字段。 UIManager.put 可以为窗口中的所有文本字段更改此背景,但我只想为其中一个字段更改此背景。

【问题讨论】:

  • 您要更改当前焦点字段的背景颜色还是文本颜色?
  • 背景颜色。所以 .setForeground 不起作用。
  • UIManager.put 在 JTextField 中有两到三个焦点键

标签: java swing colors background jtextfield


【解决方案1】:

好的,这就是我需要的:

Properties props = new Properties(); props.put("showFocusFrame", "off");     
((AbstractLookAndFeel)UIManager.getLookAndFeel()).getTheme().setProperties(prop‌​s);

【讨论】:

    【解决方案2】:

    您可以通过多种方式实现此目的,但基本思想是,当字段获得焦点时,您希望将字段背景颜色设置为其他颜色,而当它失去焦点时,您希望将其重置...

    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class FocusedField {
    
        public static void main(String[] args) {
            new FocusedField();
        }
    
        public FocusedField() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JTextField field1 = new JTextField(20);
                    JTextField field2 = new JTextField(20);
    
                    FocusListener highlighter = new FocusListener() {
    
                        @Override
                        public void focusGained(FocusEvent e) {
                            e.getComponent().setBackground(Color.GREEN);
                        }
    
                        @Override
                        public void focusLost(FocusEvent e) {
                            e.getComponent().setBackground(UIManager.getColor("TextField.background"));
                        }
                    };
    
                    field1.addFocusListener(highlighter);
                    field2.addFocusListener(highlighter);
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new GridBagLayout());
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.insets = new Insets(4, 4, 4, 4);
                    gbc.gridwidth = gbc.REMAINDER;
                    frame.add(field1, gbc);
                    frame.add(field2, gbc);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
    }
    

    我很想编写一个简单的单例“管理器”,它允许您根据需要注册和取消注册字段。

    您也可以通过将PropertyChangeListener 附加到KeyboardFocusManager 来实现类似的效果,这将允许您基本将此突出显示概念应用于任何程序中的所有字段,而无需更改任何代码,但这取决于您的要求

    【讨论】:

    • 好的,感谢所有回复,但现在我发现了我的问题的原因。在我的程序中,我使用 jtatoo 外观和感觉,它以某种方式覆盖了我编辑文本字段的背景颜色。我怎样才能删除它?
    • 你最好问问作者
    • 好的,我需要的是:Properties props = new Properties(); props.put("showFocusFrame", "off"); ((AbstractLookAndFeel)UIManager.getLookAndFeel()).getTheme().setProperties(props);
    【解决方案3】:

    只需在你的 textField 上添加一个 ActionListener,然后在 Listener 中设置背景。

    【讨论】:

    • 我在识别哪个文本字段被聚焦但改变它的背景方面没有问题。 textField.setBackground 会更改它,但是当您单击该字段以编辑背景时,会再次更改为默认值。
    【解决方案4】:

    我认为它适用于 textField.setForeground(Color.RED) :)

    【讨论】:

    • 设置字段的文字颜色
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多