Swing:字体颜色和样式JTextPane 中的删除线颜色

发布于 2024-10-22 07:43:02 字数 449 浏览 6 评论 0原文

我有一个 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);

I have a JTextPane in which text color is set as blue by default. Now i added strike-through on text then strike-through color becomes as same of text (blue). I want the color of text and strike-through different. e.g. if text color is blue then the strike-through must be different.

Please give me some idea.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷情 2024-10-29 07:43:02
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);
        }
    }
}
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);
        }
    }
}
旧时浪漫 2024-10-29 07:43:02

我认为这应该可以解决问题......

MutableAttributeSet attributes = text.getInputAttributes();
StyleConstants.setStrikeThrough(attributes , true);
StyleConstants.setForeground(attributes , Color.BLack);

StyledDocument doc = text.getStyledDocument();
doc.setCharacterAttributes(0, doc.getLength() + 1, attributes, false);

I think this should do the trick...

MutableAttributeSet attributes = text.getInputAttributes();
StyleConstants.setStrikeThrough(attributes , true);
StyleConstants.setForeground(attributes , Color.BLack);

StyledDocument doc = text.getStyledDocument();
doc.setCharacterAttributes(0, doc.getLength() + 1, attributes, false);
荒芜了季节 2024-10-29 07:43:02

或者您可以查看 StyledLabels:

http://www.jidesoft.com/products/JIDE_Common_Layer_Developer_Guide。 pdf

第 15 页...

Or you could have a look at StyledLabels:

http://www.jidesoft.com/products/JIDE_Common_Layer_Developer_Guide.pdf

At page 15...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文