JTextField的透明度不起作用 [英] Transparency for JTextField not working
问题描述
我正在处理服务器上的日志&将Opaque设置为false
时,我的JTextFields
不透明.
I'm working on a log in server & my JTextFields
aren't transparent when I set Opaque to false
.
我的代码:
//username
JTextField jUsername = new JTextField(10);
jUsername.setBounds(520, 284, 190, 25);
jUsername.setOpaque(false);
jUsername.setBorder(null);
getContentPane().add(jUsername);
//password
JTextField jPassword = new JTextField(15);
jPassword.setBounds(520, 374, 190, 25);
jPassword.setOpaque(false);
jPassword.setBorder(null);
//jPassword.setBackground(new Color(Color.TRANSLUCENT));
getContentPane().add(jPassword);
图像仍在发生什么:
任何人都从未见过或知道如何解决它?我环顾四周,但没有人遇到与我相同的问题,&他们的修补程序不适用于我的. (我知道我没有使用JPasswordField
作为密码,这是临时的)
Anyone ever seen this before or know how to fix it? I've looked around but no one had the same problem as I do, & the fixes for theirs didn't work for mine. ( I Know I'm not using JPasswordField
for password, that's temporary )
推荐答案
基本上,无论不透明设置如何,文本字段的UI委托不仅会绘制文本,还会绘制字段区域(边框内).
Basically, the UI delegate of the text field paints not only the text but also the field area (within the border) regardless of the opaque setting.
您可以做的是将背景色设置为透明值,例如new Color(0, 0, 0, 0)
之类的,它是完全透明的.
What you can do, is set the background color to a transparent value, something like new Color(0, 0, 0, 0)
for example, which is fully transparent.
例如...
JTextField jUsername = new JTextField(10);
jUsername.setBounds(520, 284, 190, 25);
jUsername.setBackground(new Color(0, 0, 0, 0));
jUsername.setOpaque(false);
jUsername.setBorder(null);
getContentPane().add(jUsername);
//password
JTextField jPassword = new JTextField(15);
jPassword.setBounds(520, 374, 190, 25);
jPassword.setBackground(new Color(0, 0, 0, 0));
jPassword.setOpaque(false);
jPassword.setBorder(null);
//jPassword.setBackground(new Color(Color.TRANSLUCENT));
getContentPane().add(jPassword);
您可以通过更改最后一个参数来影响颜色的透明度,例如new Color(255, 255, 255, 128)
将为白色,透明度为50%...
You can affect the transparency of a color by changing the last parameter, for example new Color(255, 255, 255, 128)
would white, 50% transparent...
You may also wish to change the caret color, take a look at JTextComponent#setCaretColor
for more details
这篇关于JTextField的透明度不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!