Swingr的JTextField、JPasswordField设置圆角输入框
方法1:定义Border,然后给JTextField设置即可
摘自并整理:https://blog.csdn.net/u012093968/article/details/39316679
最好添加这句话g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)告诉绘制API我们需要平滑一点,
否则绘制出来会有很多锯齿哟。
1 package com.xxx.xxx.xxx; 2 3 import java.awt.Color; 4 import java.awt.Component; 5 import java.awt.Graphics; 6 import java.awt.Graphics2D; 7 import java.awt.Insets; 8 import java.awt.RenderingHints; 9 10 import javax.swing.border.Border; 11 12 /** 13 * Swing 14 * 设置圆角边框(可以自定义边框的颜色) 15 * 可以为button,文本框等人以组件添加边框 16 * 使用方法: 17 * JButton close = new JButton(" 关 闭 "); 18 * close.setOpaque(false);// 设置原来按钮背景透明 19 * close.setBorder(new RoundBorder());黑色的圆角边框 20 * close.setBorder(new RoundBorder(Color.RED)); 红色的圆角边框 21 * 22 * @author Monsoons 23 */ 24 25 public class RoundBorder implements Border { 26 private Color color; 27 28 private int arcH = 15; 29 private int arcW = 15; 30 31 public RoundBorder() { 32 this(Color.BLACK); 33 // 如果实例化时,没有传值 34 // 默认是黑色边框 35 } 36 37 public RoundBorder(Color color) { 38 this.color = color; 39 } 40 41 public Insets getBorderInsets(Component c) { 42 43 // top:可以调节光标与边枉的距离, 间接影响高度 44 // left:可以调节光标与边枉的距离 45 // bottom:可以调节光标与边枉的距离, 间接影响高度 46 // right:可以调节光标与边枉的距离 47 return new Insets(10, 15, 10, 15); 48 } 49 50 public boolean isBorderOpaque() { 51 return false; 52 } 53 54 // 实现Border(父类)方法 55 @Override 56 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 57 // g.setColor(color); 58 // g.drawRoundRect(0, 0, c.getWidth() - 1, c.getHeight() - 1, arcH, arcW); 59 60 Graphics2D g2d = (Graphics2D) g.create(); 61 62 g2d.setColor(color); 63 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 64 g2d.drawRoundRect(0, 0, c.getWidth() - 1, c.getHeight() - 1, arcH, arcW); 65 66 g2d.dispose(); 67 } 68 }
方法二: 重写JTextField,然后覆盖paintBorder方法
摘自:https://www.aliyun.com/jiaocheng/543166.html?spm=5176.100033.2.10.SPifa6
1 /*Swing的圆角输入框 2 发布时间:2018-03-01 来源:网络 上传者:用户 3 关键字: 圆角 输入 Swing 4 发表文章 5 摘要:最近看了flyPig前辈的圆角高亮Buttonhttp://flypig.iteye.com/blog/389253,依葫芦画瓢,自己做了一个简单的圆角输入框。importjava.awt.Color;importjava.awt.FlowLayout;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.Insets;importjava.awt.RenderingHints;importjava. 6 7 最近看了flyPig前辈的圆角高亮Buttonhttp://flypig.iteye.com/blog/389253,依葫芦画瓢,自己做了一个简单的圆角输入框。*/ 8 9 import java.awt.Color; 10 import java.awt.FlowLayout; 11 import java.awt.Graphics; 12 import java.awt.Graphics2D; 13 import java.awt.Insets; 14 import java.awt.RenderingHints; 15 import java.awt.Shape; 16 import java.awt.event.MouseAdapter; 17 import java.awt.event.MouseEvent; 18 import java.awt.geom.RoundRectangle2D; 19 import javax.swing.JFrame; 20 import javax.swing.JTextField; 21 22 public class MyTextField extends JTextField { 23 private static final long serialVersionUID = -1946802815417758252L; 24 25 public MyTextField(int columns) { 26 super(columns); 27 // 28 setMargin(new Insets(0, 5, 0, 5)); 29 } 30 31 @Override 32 protected void paintBorder(Graphics g) { 33 int h = getHeight();// 从JComponent类获取高宽 34 int w = getWidth(); 35 Graphics2D g2d = (Graphics2D) g.create(); 36 Shape shape = g2d.getClip(); 37 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 38 g2d.setClip(shape); 39 g2d.drawRoundRect(0, 0, w - 2, h - 2, h, h); 40 g2d.dispose(); 41 super.paintBorder(g2d); 42 } 43 44 public static void main(String[] args) { 45 JFrame jf = new JFrame(); 46 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 47 jf.setSize(300, 200); 48 jf.setLayout(new FlowLayout()); 49 MyTextField text = new MyTextField(20); 50 jf.add(text); 51 jf.setVisible(true); 52 } 53 }