2012-02-29 79 views
1

我正在實現一個拼寫檢查器,並想知道是否有一種簡單/明顯的方式來強調某些不同顏色的文本,如紅色。用AttributeSet設置不同顏色的下劃線樣式常量

我把一切都建立並用下面的代碼,強調(也設置文本的顏色):

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

上述代碼設置的字爲藍色,但也強調爲藍色。我需要能夠改變下劃線和厚度。有任何想法嗎?

感謝,

回答

5

使用e.g這 http://java-sl.com/tip_colored_strikethrough.html

只需稍作修改以達到下劃線,而不是刪除線。

但是從我的經驗,突出拼寫錯誤,這是更好地定義自定義的亮點畫家

參見 http://www.java2s.com/Code/Java/Swing-JFC/JTextPaneHighlightExample.htm

+0

感謝您的答覆。在自定義高亮顯示畫筆中,顯示了以下內容:paintLayer(Graphics g,int offs0,int offs1,Shape bounds, JTextComponent c,View view) – maloney 2012-03-01 09:29:38

+0

+1多數民衆贊成有用...只是用它作爲下劃線通過簡單更改y方程。 – 2012-08-22 12:11:17