欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Swing中Laber添加下划线 博客分类: 图形界面 label下划线 

程序员文章站 2024-03-25 18:56:04
...

下划线的颜色可以单独定义

import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class JLabelUnderLinePerfect extends JLabel {

	private Color underLineColor;

	public JLabelUnderLinePerfect() {
		this("");
	}

	public JLabelUnderLinePerfect(String text) {
		super(text);
	}

	public Color getUnderLineColor() {
		return underLineColor;
	}

	public void setUnderLineColor(Color pUnderLineColor) {
		underLineColor = pUnderLineColor;
	}

	public void paint(Graphics g) {
		super.paint(g);

		Rectangle r = g.getClipBounds();
		int xoffset = 0, yoffset = 0, pointX = 0, pointY = 0, point2X = 0, point2Y = 0;

		// 根据border的设置 计算出下划线的起止Point
		if (null != this.getBorder()
				&& null != this.getBorder().getBorderInsets(this)) {
			Insets inserts = this.getBorder().getBorderInsets(this);
			xoffset = inserts.left;
			yoffset = inserts.bottom;
		}
		pointX = xoffset;
		pointY = point2Y = r.height - yoffset
				- getFontMetrics(getFont()).getDescent();
		point2X = pointX + getFontMetrics(getFont()).stringWidth(getText());
		if (null != underLineColor) {
			g.setColor(underLineColor);
		}

		g.drawLine(pointX, pointY, point2X, point2Y);
	}

	public static void main(String[] args) {
		JFrame f = new JFrame("JLabe with Under Line");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(new Dimension(300, 200));
		f.setLayout(new FlowLayout());

		JLabelUnderLinePerfect label1 = new JLabelUnderLinePerfect("Label下划线");
		f.add(label1);

		JLabelUnderLinePerfect label2 = new JLabelUnderLinePerfect("Label下划线");
		label2.setUnderLineColor(Color.BLUE);
		f.add(label2);

		JLabelUnderLinePerfect label3 = new JLabelUnderLinePerfect(
				"Label(Border)下划线");
		label3.setBorder(BorderFactory.createCompoundBorder(new LineBorder(
				Color.RED), BorderFactory.createEmptyBorder(8, 18, 28, 38)));
		label3.setUnderLineColor(Color.BLUE);
		f.add(label3);

		// f.pack();
		f.setVisible(true);

	}
}

 

相关标签: label 下划线