【问题标题】:Underline StyleConstant in a different colour with AttributeSet使用 AttributeSet 在不同颜色下划线 StyleConstant
【发布时间】:2012-03-19 03:32:23
【问题描述】:

我正在实现一个拼写检查器,并想知道是否有一种简单/明显的方法可以用不同的颜色(例如红色)为某些文本加下划线。

我已经用以下代码设置了所有内容并加了下划线(也设置了文本的颜色):

private AttributeSet getAttributeSet(Color foregroundColor) {
    SimpleAttributeSet attrs = new SimpleAttributeSet();
    StyleConstants.setForeground(attrs, foregroundColor);
    StyleConstants.setUnderline(attrs, true);
}

上面的代码将单词设置为蓝色,但也用蓝色下划线。我需要能够更改下划线及其厚度。有什么想法吗?

谢谢,

【问题讨论】:

    标签: java swing jtextcomponent


    【解决方案1】:

    使用例如这个 http://java-sl.com/tip_colored_strikethrough.html

    只需稍微修改一下即可实现下划线而不是删除线。

    但根据我的经验,要突出拼写检查错误,最好定义自定义突出显示画家

    另请参阅 http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneHighlightExample.htm

    【讨论】:

    • 感谢您的回复。在自定义高亮画家中,进入:paintLayer(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c, View view)
    • +1 这很有用...只是通过对 y 方程进行简单更改来将其用于下划线。
    猜你喜欢
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 2016-09-03
    • 2019-02-13
    • 2010-09-16
    • 2019-03-04
    相关资源
    最近更新 更多