简体   繁体   English

JTextField-如何设置背景颜色?

[英]JTextField - how to set background color?

I want to know how to set color of my JTextField component. 我想知道如何设置我的JTextField组件的颜色。 I tried this way: 我这样尝试:

setBackground(Color.white)

and it sets a white color, but when the field is marked. 并设置为白色,但标记了该字段。 I want the field to be white immediately after my program starts and without any user interaction. 我希望程序启动后立即将该字段设置为白色,并且无需任何用户交互。

Thanks for help! 感谢帮助!

You can change background color by this code 您可以通过此代码更改背景颜色

textField.setBackground(Color.RED);

You should also check this Change JTextField enabled background color 您还应该检查启用更改JTextField的背景色

I cannot replicate the problem you're having. 我无法复制您遇到的问题。 Perhaps you are initially seeing the background color of the component holding the JTextField, rather than the JTextField's background. 也许您最初看到的是持有JTextField的组件的背景颜色,而不是JTextField的背景。

JFrame f = new JFrame();
f.setBackground(Color.BLUE);
f.setLayout(new GridLayout(1, 1));

JTextField tf = new JTextField();
tf.setBackground(Color.GREEN);
f.add(tf);

f.setSize(500, 500);
f.setVisible(true);

Thread.sleep(2500);
tf.setBackground(Color.RED);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM