Swing:字体颜色&在JTextPane中打击颜色

时间:2011-03-17 09:55:11

标签: 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);

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

或者您可以查看StyledLabels:

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

在第15页......