我有一个更改 JTextField 颜色的代码片段:
textField.setBackground(new Color(155, 0, 0, 155));
当颜色改变时,组件有一个奇怪的行为:光标闪烁变得不规则,因此颜色不透明度似乎在正在进行的重绘事件中发生变化。如果在没有指定 alpha 参数的情况下创建颜色,则该错误似乎消失了。
关于如何在 JTextField 中使用半透明颜色有什么想法吗?
最佳答案
我在Sun网站上读到一篇文章:Unleash Your Creativity with Swing and the Java 2D API!
[...]The third translucent painting technique is the most powerful, allowing for arbitrary painting to be done translucently. By invoking setComposite on a Graphics2D object with an instance of java.awt.AlphaComposite, you can specify a translucency level to be used in subsequent paint calls.[...]"
我尝试在paintComponent上下文中使用这种技术,并且成功地使用了半透明颜色。 (RTFM...我就知道!)
关于java - 如何在 jtextfield 上设置半透明背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2160697/