JTextField边框设置-美化

JTextField要想实现边框美化 就必须重写方法,下面是我自己用的一个边框工具类(你也可以自己修改)。代码如下:


import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.border.LineBorder;

/**
 * @author user 
 *   边框设置	
 */

public class TextBorderUtlis extends LineBorder 
{

	private static final long serialVersionUID = 1L;

	public TextBorderUtlis(Color color, int thickness, boolean roundedCorners) 
	{
		super(color, thickness, roundedCorners);
	}

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

		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		Color oldColor = g.getColor();
		Graphics2D g2 = (Graphics2D) g;
		int i;
		g2.setRenderingHints(rh);
		g2.setColor(lineColor);
		for (i = 0; i < thickness; i++) 
		{
			if (!roundedCorners){
				g2.drawRect(x + i, y + i, width - i - i - 1, height - i - i - 1);
			}else{
				g2.drawRoundRect(x + i, y + i, width - i - i - 1, height - i - i - 1, 5, 5);}
		}
		g2.setColor(oldColor);
	}

}

然后你们可以直接调用这个,如图:
JTextField边框设置-美化_第1张图片

你可能感兴趣的:(JAVA,Swing)