┏(ω)=☞ 本专栏的目录(为您提供更好的查询方式)(点这里说不定有你想要的)
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();
}
}