【问题标题】:How do I set different colors for text and underline in JTextPane?如何在 JTextPane 中为文本和下划线设置不同的颜色?
【发布时间】:2012-08-17 19:41:20
【问题描述】:

刚刚尝试为 JTextPane 中的文本着色 - 但问题是文本和下划线不能有不同的颜色。我应该怎么做,或者这甚至可能吗?下面的示例以 RED 打印所有文本和下划线。

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);

【问题讨论】:

标签: java swing highlighting jtextpane


【解决方案1】:

这是我找到的解决方案...

Underline StyleConstant in a different colour with AttributeSet

这里是示例代码的链接

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

只需更改以下行即可解决它...

int y = a.getBounds().y + a.getBounds().height + 2 ; 

效果很好

【讨论】:

    【解决方案2】:

    基本上你需要创建 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

    这个答案主要是为了后代,我认为添加链接代码和解释的简化版本将有助于使事情更容易理解。

    【讨论】:

      【解决方案3】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-11
        • 2012-08-30
        • 2012-10-15
        • 1970-01-01
        • 2016-11-24
        • 2011-08-29
        相关资源
        最近更新 更多