2012-08-22 46 views
5

剛試圖在JTextPane中對文本着色 - 但問題是不能爲文本和下劃線使用不同的顏色。我該怎麼做,或者甚至有可能?下面的示例打印所有文本並在RED中加下劃線。如何爲JTextPane中的文本和下劃線設置不同的顏色?

JTextPane pane = new JTextPane(); 

StyleContext context = new StyleContext(); 

Style style = pane.addStyle("Black", null); 
StyleConstants.setAlignment(style, StyleConstants.ALIGN_RIGHT); 
StyleConstants.setFontSize(style, 14); 
StyleConstants.setSpaceAbove(style, 4); 
StyleConstants.setSpaceBelow(style, 4); 
StyleConstants.setForeground(style, Color.BLACK); 

StyledDocument document = pane.getStyledDocument(); 


style = pane.addStyle("Red Underline", style); 
StyleConstants.setForeground(style, Color.RED); 
StyleConstants.setUnderline(style, true); 

pane.getDocument().insertString(0, "Test String", style); 
+0

+1儘管樣式API似乎是可擴展的,但我找不到任何文檔如何去做。 –

+0

在這裏找到答案... http:// stackoverflow。com/questions/9502654 /下劃線 - styleconstant-in-a-different-color-with-attributeset –

+0

發佈作爲回答 –

回答

4

基本上有3類,您需要創建:

  • 但是,您需要擴展javax.swing.text.LabelView以執行修改視圖,但是您希望(無論是否添加彩色下劃線)。您將覆蓋paint(Graphics, Shape)方法。您可以在重寫的類中使用此行訪問屬性 - 這些屬性應該是對文本進行額外操作的觸發器(如添加下劃線)。

    getElement().getAttributes().getAttribute("attribute name");

  • 您需要創建一個新的ViewFactory並覆蓋create方法。這是,這樣做,當你處理所有的元素類型很重要(否則事情不會很顯示正確。

  • 你需要創建一個StyledEditorKit,使確定要使用哪個ViewFactory窗格。

這裏的這種簡化的和可運行例如:

import java.awt.*; 
import javax.swing.*; 
import javax.swing.plaf.basic.BasicTextPaneUI; 
import javax.swing.text.*; 

public class TempProject extends JPanel{ 


    public static void main(String args[]) { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       //Adding pane 
       JTextPane pane = new JTextPane(); 
       pane.setEditorKit(new CustomEditorKit()); 
       pane.setText("Underline With Different Color"); 

       //Set Style 
       StyledDocument doc = (StyledDocument)pane.getDocument(); 
       MutableAttributeSet attrs = new SimpleAttributeSet(); 
       attrs.addAttribute("Underline-Color", Color.red); 
       doc.setCharacterAttributes(0, doc.getLength()-1, attrs, true); 

       JScrollPane sp = new JScrollPane(pane); 
       frame.setContentPane(sp); 
       frame.setPreferredSize(new Dimension(400, 300)); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 


      } 
     }); 
    } 

    public static class CustomEditorKit extends StyledEditorKit{ 

     public ViewFactory getViewFactory(){ 
      return new CustomUI(); 
     } 
    } 

    public static class CustomUI extends BasicTextPaneUI{ 
     @Override 
     public View create(Element elem){ 
      View result = null; 
      String kind = elem.getName(); 
      if(kind != null){ 
       if(kind.equals(AbstractDocument.ContentElementName)){ 
        result = new MyLabelView(elem); 
       } else if(kind.equals(AbstractDocument.ParagraphElementName)){ 
        result = new ParagraphView(elem); 
       }else if(kind.equals(AbstractDocument.SectionElementName)){ 
        result = new BoxView(elem, View.Y_AXIS); 
       }else if(kind.equals(StyleConstants.ComponentElementName)){ 
        result = new ComponentView(elem); 
       }else if(kind.equals(StyleConstants.IconElementName)){ 
        result = new IconView(elem); 
       } else{ 
        result = new LabelView(elem); 
       } 
      }else{ 
       result = super.create(elem); 
      } 

      return result; 
     } 
    } 

    public static class MyLabelView extends LabelView{ 

     public MyLabelView(Element arg0) { 
      super(arg0); 
     } 

     public void paint(Graphics g, Shape a){ 
      super.paint(g, a); 
      //Do whatever other painting here; 
      Color c = (Color)getElement().getAttributes().getAttribute("Underline-Color"); 
      if(c != null){ 
       int y = a.getBounds().y + (int)getGlyphPainter().getAscent(this); 
       int x1 = a.getBounds().x; 
       int x2 = a.getBounds().width + x1; 

       g.setColor(c); 
       g.drawLine(x1, y, x2, y); 
      } 

     } 

    } 

} 

這裏的鏈接到另一個示例代碼:

http://java-sl.com/tip_colored_strikethrough.html

這個答案主要是爲了後人,我想添加一個簡化版的鏈接代碼和解釋將有助於使事情變得更容易理解。

+0

好描述+1 – mKorbel

相關問題