java - Swing : font color & strike color in JTextPane

标签 java swing fonts jtextpane

我有一个 JTextPane,其中文本颜色默认设置为蓝色。现在我在文本上添加了删除线,然后删除线颜色变得与文本相同(蓝色)。我想要文本的颜色和删除线不同。例如如果文本颜色为蓝色,则删除线必须不同。

请给我一些想法。

    JTextPane text = new JTextPane();

    Font font = new Font("Serif", Font.ITALIC, 20);
    text.setFont(font); 

    text.setForeground(Color.BLUE); 

    Style style = text.addStyle("Bold", null);
    StyleConstants.setStrikeThrough(style, true);

    text.setCharacterAttributes(style, false);

最佳答案

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class Test {

    public Test() {
        JFrame fr = new JFrame("TEST");
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JEditorPane pane = new JEditorPane();
        pane.setEditorKit(new NewEditorKit());
        pane.setText("test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test ");

        StyledDocument doc = (StyledDocument) pane.getDocument();
        MutableAttributeSet attr = new SimpleAttributeSet();
        attr.addAttribute("strike-color", Color.red);
        doc.setCharacterAttributes(0, 9, attr, false);

        attr.addAttribute("strike-color", Color.blue);
        doc.setCharacterAttributes(10, 19, attr, false);
        JScrollPane sp = new JScrollPane(pane);

        fr.getContentPane().add(sp);
        fr.setSize(300, 300);
        fr.setLocationRelativeTo(null);
        fr.setVisible(true);
    }

    public static void main(String[] args) {
        Test test = new Test();
    }
}

class NewEditorKit extends StyledEditorKit {
    public ViewFactory getViewFactory() {
        return new NewViewFactory();
    }
}

class NewViewFactory implements ViewFactory {
    public View create(Element elem) {
        String kind = elem.getName();
        if (kind != null) {
            if (kind.equals(AbstractDocument.ContentElementName)) {
                return new MyLabelView(elem);
            }
            else if (kind.equals(AbstractDocument.ParagraphElementName)) {
                return new ParagraphView(elem);
            }
            else if (kind.equals(AbstractDocument.SectionElementName)) {
                return new BoxView(elem, View.Y_AXIS);
            }
            else if (kind.equals(StyleConstants.ComponentElementName)) {
                return new ComponentView(elem);
            }
            else if (kind.equals(StyleConstants.IconElementName)) {
                return new IconView(elem);
            }
        }

        // default to text display
        return new LabelView(elem);
    }
}

class MyLabelView extends LabelView {

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

    public void paint(Graphics g, Shape allocation) {
        super.paint(g, allocation);
        paintStrikeLine(g, allocation);
    }

    public void paintStrikeLine(Graphics g, Shape a) {
        Color c=(Color)getElement().getAttributes().getAttribute("strike-color");
        if (c!=null) {
            int y = a.getBounds().y + a.getBounds().height - (int) getGlyphPainter().getDescent(this);
            y = y - (int) (getGlyphPainter().getAscent(this) * 0.3f);
            int x1 = (int) a.getBounds().getX();
            int x2 = (int) (a.getBounds().getX() + a.getBounds().getWidth());

            Color old = g.getColor();
            g.setColor(c);
            g.drawLine(x1, y, x2, y);
            g.setColor(old);
        }
    }
}

关于java - Swing : font color & strike color in JTextPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5337444/

相关文章:

java - 这个 Java 对工作问卷的回复有效吗?

java - JPopupMenu 中的 JRadioButton 选择后不关闭菜单

css - 如何向自定义 magento 主题添加新字体?

html - 谷歌字体在实时服务器上工作但不在本地主机上工作

java - http 多部分发布请求,包含 json 和带有 koush/ion 库的图像

java - 具有优先级的 sortOn 函数

java - 从 JFrame 程序创建 JApplet

html - 有没有办法将字体添加到 Assets 文件夹中并为 html 加载它?

java - hadoop 中多种输入格式作为单一输入格式

java - 单击按钮时,应打开新窗口(内部框架),我的代码有什么问题吗?