如何设置字体的颜色? [英] How to set the color of a Font?

查看:66
本文介绍了如何设置字体的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JList,并且在该列表内,我想更改以下单词的字体颜色.

I have a JList and inside that list, I want to change the color of the font of the following words.

有人说我必须使用CellRenderer,但是我还不熟悉.

Someone said that I have to use CellRenderer but I'm not yet familiar with that.

现在,我的问题是如何设置此渲染器以更改字体的颜色?

Now, my problem is how can I setup this renderer to change the color of the Font?

对此事有任何想法吗?

谢谢...

推荐答案

查看

Check out the JavaDoc, it might just have what you need (the example at the top). Basically you just return a component (like a JLabel) with a configured font color.

由于您想使单词的颜色与其定义不同,因此这里有两种可能性可以使用列表单元格渲染器完成:

since you want to have the words in a different color than their definitions, here two possibilities how that can be done with the list cell renderer:

  1. 返回一个包含2个JLabel的JPanel,一个包含单词,一个包含定义.两个标签可以具有不同的前景色.
  2. JLabel支持HTML(就像其他Swing组件一样),因此您可以将标签的文本设置为<html><font color=red>your word</font> - your definitions</html>.请注意,默认的前景色应为定义的一种,或者,您也可以使用<font>标签包装这些定义.看看此教程.
  1. Return a JPanel that contains 2 JLabels, one with the word and one with the definitions. Both labels can have different foreground colors.
  2. JLabel supports HTML (like other Swing components do), so you might just set the text of the label as <html><font color=red>your word</font> - your definitions</html>. Note that the default foreground color should then be the one for the definitions, alternatively you can wrap the definitions with a <font> tag as well. Have a look at this Tutorial.

这篇关于如何设置字体的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆