如何在JTextPane中设置不同的文本和下划线颜色?

5

我刚尝试给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);

尽管Style API似乎是可扩展的,但我找不到任何关于如何做到这一点的文档。+1 - Aaron Digulla
在这里找到答案...http://stackoverflow.com/questions/9502654/underline-styleconstant-in-a-different-colour-with-attributeset - Sid Malani
3个回答

5

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

这篇回答基本上是为后人而写的,我认为添加链接代码和解释的简化版本将有助于使事情更易懂。

2
  • 例如,以及为Html

  • Document返回模型到视图,可以确定行开始/结束的索引


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接