我尝试将 Color 添加到 java.awt.Font 的属性中,如下所示:
font.getAttributes().put(TextAttribute.FOREGROUND, jColorChooser.getColor());
但是我得到了错误
The method put(TextAttribute, capture#12-of ?) in the type Map is not applicable for the arguments (TextAttribute, Color)
This Font only recognizes keys defined in TextAttribute as attributes And FOREGROUND is a present in TextAttribute
我做错了什么吗?
最佳答案
事实上,要更改字体,您不能直接更改其属性,因为 Swing 字体应该是不可变的。
因此,您必须调用它的 Font#deriveFont(Map)
具有新属性集的方法。
这将创建具有给定属性集的新字体,并因此创建新颜色。
关于java - 将颜色添加到字体的属性java swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3761209/