Java——TextField边框美化

┏(ω)=☞ 本专栏的目录(为您提供更好的查询方式)(点这里说不定有你想要的)

paintBorder的方法重写,使用的时候直接调用这个类就可以了

这个是my类

package TYZ;

import java.awt.*;

import javax.swing.border.AbstractBorder;

public class MyTextFieldBorder extends AbstractBorder {
	private Color color;

	public MyTextFieldBorder(Color color) {
		this.color=color;
	}
	public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setColor(color);//设置的颜色
		g2.drawRoundRect(2, 0, c.getWidth() - 4, c.getHeight() - 4, 30, 30);//文本框水平、垂直、宽、高、弧度宽、高
	}

	public Insets getBorderInsets(Component c) {
		return new Insets(0, 10, 4, 0);//设置文本里的文字位置,分别为字体往下偏、往右偏、往上偏、往左偏
	}
}

注意在实际操作的时候my类放在哪

简单的演示代码,如下:

package TYZ;

import java.awt.Color;
import javax.swing.*;

public class text1 extends JFrame{
	public text1() {
		setSize(200,200);//设置JFrame大小
		setLocationRelativeTo(null);//设置JFrame居中
		setDefaultCloseOperation(3);//设置关闭窗口后程序停止运行
		setLayout(null);//设置绝对定位
		JTextField jTextField=new JTextField("嘻嘻嘻");//创建文本框
		jTextField.setBounds(50,50,100,30);//设置文本框大小及位置
		jTextField.setOpaque(false);//设置透明
		jTextField.setBorder(new my(Color.red));//调用my类
		add(jTextField);//添加到JFrame中
		setVisible(true);//设置窗口可见
	}
	public static void main(String[] args) {
		new text1();
	}
}

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桂?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值