1

我知道 StyleConstants 中有一种方法可以用一个下划线设置文本。

但是如果我想设置带有双下划线的文本怎么办?

有什么简单的方法吗?

4

2 回答 2

1

http://java-sl.com/tip_colored_strikethrough.html 该链接显示彩色删除线,但您可以使用相同的方法并绘制 2 条线

import javax.swing.*; 

import javax.swing.text.*; 
import java.awt.*; 
 
public class ColoredStrikeThroughText { 
 
    public ColoredStrikeThroughText() { 
        JFrame fr = new JFrame("Custom color StrikeThrough attribute"); 

        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
 
        JEditorPane pane = new JEditorPane(); 
        pane.setEditorKit(new StyledEditorKit() { 
            public ViewFactory getViewFactory() { 
                return new NewViewFactory(); 

            } 
        }); 
        pane.setText("Color red text, color blue text, text without coloring."); 
 
        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(17, 27, 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) { 
        ColoredStrikeThroughText test = new ColoredStrikeThroughText(); 
    } 
} 

 
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); 
        } 
    } 
} 
于 2012-11-25T06:13:11.860 回答
0

您将在 CSS 元素中输入 { text-decoration: underline double;}

例如:p { text-decoration: underline double;}

于 2021-08-30T22:16:05.027 回答