2012-08-22 43 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

相关问题