改变JTextField启用时的背景颜色

3

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


您想要更改当前聚焦字段的背景颜色还是文本颜色? - MadProgrammer
背景颜色。所以.setForeground不起作用。 - clsbartek
UIManager.put在JTextField中有两到三个用于焦点的键。 - mKorbel
4个回答

12

有许多方法可以实现这一点,但基本思想是,在字段获得焦点时,您希望将字段的背景颜色设置为其他颜色,并在失去焦点时将其重置...

FocusChange

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来实现类似的功能,这将使您能够基本上将此突出显示概念应用于任何程序中的所有字段,而无需更改任何代码,但这取决于您的要求。


好的,感谢所有的回复,但现在我发现了问题的原因。在我的程序中,我使用了JTattoo外观,并且它以某种方式覆盖了我的编辑文本框的背景颜色。我该如何移除它? - clsbartek
你最好的选择是询问作者。 - MadProgrammer
好的,这是我需要的: Properties props = new Properties(); props.put("showFocusFrame", "off"); ((AbstractLookAndFeel)UIManager.getLookAndFeel()).getTheme().setProperties(props); - clsbartek

0

好的,这就是我需要的:

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

0
只需在您的textField上添加一个ActionListener,然后在监听器中设置背景即可。

我不是无法识别哪个文本字段处于焦点,而是无法更改其背景。textField.setBackground可以更改背景,但当您单击该字段进行编辑时,背景会再次更改为默认值。 - clsbartek

-1

我认为使用 textField.setForeground(Color.RED) 可以实现这个功能 :)


设置字段的文本颜色。 - MadProgrammer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接