Java 更改启用 JTextField 的背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24880068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change JTextField enabled background color
提问by clsbartek
i've got a question about JTextField
background color. How can I change it in enabled text field (while editing) ? setBackground
works only for disabled text field. UIManager.put
can change this background for all of my text field in window but i want to do that only for one of them.
我有一个关于JTextField
背景颜色的问题。如何在启用的文本字段中更改它(编辑时)?setBackground
仅适用于禁用的文本字段。UIManager.put
可以为窗口中的所有文本字段更改此背景,但我只想为其中之一执行此操作。
采纳答案by clsbartek
Ok, there is what i needed:
好的,有我需要的:
Properties props = new Properties(); props.put("showFocusFrame", "off");
((AbstractLookAndFeel)UIManager.getLookAndFeel()).getTheme().setProperties(prop??s);
回答by romaneso
I think it works with textField.setForeground(Color.RED)
:)
我认为它适用于textField.setForeground(Color.RED)
:)
回答by romaneso
Just add an ActionListener on your textField, and then set the Background in the Listener.
只需在 textField 上添加一个 ActionListener,然后在 Listener 中设置 Background。
回答by MadProgrammer
There's a number of ways you might achieve this, but the basic idea is, when the field gets focus, you want to set the fields background color to something else and when it loses focus, you want to reset it...
有多种方法可以实现这一点,但基本思想是,当字段获得焦点时,您希望将字段背景颜色设置为其他颜色,当它失去焦点时,您希望将其重置...
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);
}
});
}
}
I would be tempted to write a simple singleton "manager" which allowed you to register and unregister fields as you needed.
我很想编写一个简单的单例“管理器”,它允许您根据需要注册和取消注册字段。
You might also be able to achieve something similar by attaching a PropertyChangeListener
to the KeyboardFocusManager
, this would allow you to basic apply this highlighting concept to all fields within in any program without the need to change any of the code, but that would depend on your requirements
您也可以通过将 a 附加到 来实现类似PropertyChangeListener
的功能KeyboardFocusManager
,这将允许您将此突出显示概念基本应用于任何程序中的所有字段,而无需更改任何代码,但这取决于您的要求