任亮java_Java - 在setBorder中设置字体/颜色

该博客介绍了如何创建一个名为MultiColorTitleBorder的自定义Java组件边框,允许在边框中设置多颜色的标题。通过继承AbstractBorder,实现了在边框上绘制带有不同颜色的文本,并能兼容其他Border的嵌套使用,如LineBorder。示例代码展示了一个带有这种边框的JPanel,背景色为黄色,边框为 cyan 色,包含一个 JTextField 输入框。
摘要由CSDN通过智能技术生成

import java.awt.Color;

import java.awt.Component;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.FontMetrics;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Insets;

import java.awt.Rectangle;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.border.AbstractBorder;

import javax.swing.border.Border;

import javax.swing.border.LineBorder;

import javax.swing.text.BadLocationException;

import javax.swing.text.View;

public class MultiColorTitleBorder extends AbstractBorder

{

private static final long serialVersionUID = 1L;

private JLabel label;

private int thicknessTop = 10;

private Border border;

private int thicknessLeft = 0;

private int thicknessRight = 0;

private int thicknessBottom = 0;

public MultiColorTitleBorder(String title)

{

this.label = new JLabel(title);

thicknessTop = label.getPreferredSize().height;

}

public MultiColorTitleBorder(String title, Border border)

{

this(title);

this.border = border;

thicknessLeft = border.getBorderInsets(null).left;

thicknessRight = border.getBorderInsets(null).right;

thicknessBottom = border.getBorderInsets(null).bottom;

}

@Override

public synchronized void paintBorder(Component c, Graphics g, int x, int y, int width, int height)

{

Graphics2D g2 = (Graphics2D) g;

View view = (View) label.getClientProperty("html");

String text = label.getText();

FontMetrics fm = g2.getFontMetrics(label.getFont());

int bY = y + fm.getAscent() - ((fm.getAscent() + fm.getDescent())) / 2;

if(border != null)

{

Insets in = border.getBorderInsets(c);

g2.setClip(x, y, thicknessLeft * 2, height);

border.paintBorder(c, g, x, bY, width, height - bY);

try

{

if(view != null)

text = view.getDocument().getText(0, view.getDocument().getLength());

}catch(BadLocationException ex)

{

Logger.getLogger(MultiColorTitleBorder.class.getName()).log(Level.SEVERE, null, ex);

}

int textLengthInPixels = fm.stringWidth(text);

System.out.println("textLengthInPixels=" + textLengthInPixels);

g2.setClip(x +thicknessLeft * 2+ textLengthInPixels, y, width - thicknessLeft * 2 -textLengthInPixels, height);

border.paintBorder(c, g, x, bY, width, height - bY);

int bottomIn = in.bottom;

g2.setClip(x, height - bottomIn, width, bottomIn);

border.paintBorder(c, g, x, bY, width, height - bY);

g2.setClip(x, y, width, height);

}

if(view != null)

view.paint(g2, new Rectangle(x + thicknessLeft * 2, y, width - thicknessLeft * 2, height));

else

{

Font prevFont = g2.getFont();

g2.setFont(label.getFont());

g2.drawString(text, x + thicknessLeft * 2, fm.getAscent());

g2.setFont(prevFont);

}

}

@Override

public Insets getBorderInsets(Component c)

{

return new Insets(thicknessTop, thicknessLeft, thicknessBottom, thicknessRight);

}

@Override

public Insets getBorderInsets(Component c, Insets insets)

{

insets.top = thicknessTop;

insets.left = thicknessLeft;

insets.right = thicknessRight;

insets.bottom = thicknessBottom;

return insets;

}

@Override

public boolean isBorderOpaque()

{

return false;

}

public static void main(String[] args)

{

JPanel p = new JPanel();

p.setPreferredSize(new Dimension(200, 200));

String title = " Text 1      Text 2

//title = " Text 1      Text 2";

//title = "Text 1 Text 2";

p.setBorder(new MultiColorTitleBorder(title, new LineBorder(Color.CYAN, 6)));

p.setBackground(Color.YELLOW);

p.add(new JTextField(5));

JPanel contentPane = new JPanel();

contentPane.add(p);

JFrame f = new JFrame();

f.setContentPane(contentPane);

f.setSize(800, 600);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值