java.swing标签下划线,Java Swing - 如何在JTextPane中为文本和下划线设置不同的颜色...

该博客展示了一个Java Swing应用,创建了一个自定义的文本编辑器组件`CustomEditorKit`,使得在`JTextPane`中能够设置文字下划线颜色。通过重写`LabelView`类并添加`paint`方法来绘制特定颜色的下划线。程序创建了一个窗口,添加了`JTextPane`,并设置了初始文本,用红色下划线强调了所有字符。

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Shape;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTextPane;

import javax.swing.plaf.basic.BasicTextPaneUI;

import javax.swing.text.AbstractDocument;

import javax.swing.text.BoxView;

import javax.swing.text.ComponentView;

import javax.swing.text.Element;

import javax.swing.text.IconView;

import javax.swing.text.LabelView;

import javax.swing.text.MutableAttributeSet;

import javax.swing.text.ParagraphView;

import javax.swing.text.SimpleAttributeSet;

import javax.swing.text.StyleConstants;

import javax.swing.text.StyledDocument;

import javax.swing.text.StyledEditorKit;

import javax.swing.text.View;

import javax.swing.text.ViewFactory;

public class Main {

public static void main(String args[]) {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTextPane pane = new JTextPane();

pane.setEditorKit(new CustomEditorKit());

pane.setText("Underline With Different Color");

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

}

}

class CustomEditorKit extends StyledEditorKit {

public ViewFactory getViewFactory() {

return new CustomUI();

}

}

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;

}

}

class MyLabelView extends LabelView {

public MyLabelView(Element arg0) {

super(arg0);

}

public void paint(Graphics g, Shape a) {

super.paint(g, a);

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

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值