Java Swing 边框工具类

最近用到了swing写客户端小程序,写了个边框工具类:
先看效果图:
在这里插入图片描述
工具类代码:

package top.jacktgq.utils;

import java.awt.Color;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
/**
 * @author 糖果墙
 * @createDate 2019年8月15日 下午8:40:39
 * @description 边框外边距和内填充工具类
 *
 */
public class CandyBorderUtils {
	//设置填充,上左下右都设置为相同的值
	public static void addPadding(JComponent c, int size) {
		addPadding(c,size,size,size,size);
	}
	
	//设置填充,上左下右都设置为不同的值
	public static void addPadding(JComponent c, int top, int left, int bottom, int right) {
		//获取该容器原来的边框
		Border oldBorder = c.getBorder();
		//创建一个用于内填充的空边框
		Border border = BorderFactory.createEmptyBorder(top,left,bottom,right);
		//如果该组件本来就具备边框,把原边框和内填充边框进行复合
		if(oldBorder != null) {
			border = BorderFactory.createCompoundBorder(oldBorder, border);
		}
		c.setBorder(border);
	}
	
	//设置外边距,上左下右都设置为相同的值
	public static void addMargin(JComponent c, int size) {
		addMargin(c,size,size,size,size);
	}
	
	//设置外边距,上左下右都设置为不同的值
	public static void addMargin(JComponent c, int top, int left, int bottom, int right) {
		//创建一个表示外边距的空边框
		Border border = BorderFactory.createEmptyBorder(top,left,bottom,right);
		//获取该容器原来的边框
		Border oldBorder = c.getBorder();
		//如果该组件本来就具备边框,把外边距边框和原边框进行复合
		if(oldBorder != null) {
			border = BorderFactory.createCompoundBorder(border, oldBorder);
		}
		c.setBorder(border);
	}
	
	private static void addTitledborder(JComponent c, 
					    int marginBorderTop,
					    int marginBorderLeft,
					    int marginBorderBottom,
					    int margintBorderRight,
					    TitledBorder titledBorder,
					    int paddingBorderTop,
					    int paddingBorderLeft,
					    int paddingBorderBottom,
					    int paddingBorderRight) {
		//外边距边框
		Border marginBorder = BorderFactory.createEmptyBorder(marginBorderTop, marginBorderLeft, marginBorderBottom, margintBorderRight);
		//内填充边框
		Border paddingBorder = BorderFactory.createEmptyBorder(paddingBorderTop, paddingBorderLeft, paddingBorderBottom, paddingBorderRight);
		CompoundBorder compoundBorder = BorderFactory.createCompoundBorder(marginBorder,titledBorder);
		compoundBorder = BorderFactory.createCompoundBorder(compoundBorder,paddingBorder);
		c.setBorder(compoundBorder);
	}
	
	//给不包含边框的容器设置带有标题的边框,并且可以指定内外边距(上左右下都为相同的值)
	public static void addTitledBorder(JComponent c, 
					   int marginBorderSize,
					   String title,
					   int paddingBorderSize) {
		addTitledBorder(c, marginBorderSize, marginBorderSize, marginBorderSize, marginBorderSize, 
				title, paddingBorderSize, paddingBorderSize, paddingBorderSize, paddingBorderSize);
	}
	
	//给不包含边框的容器设置带有标题的边框,并且可以指定内外边距(上左右下都为不同的值)
	public static void addTitledBorder(JComponent c, 
					   int marginBorderTop,
					   int marginBorderLeft,
					   int marginBorderBottom,
					   int margintBorderRight,
					   String title,
					   int paddingBorderTop,
					   int paddingBorderLeft,
					   int paddingBorderBottom,
					   int paddingBorderRight) {
		//标题边框
		TitledBorder titledBorder = BorderFactory.createTitledBorder(title);
		addTitledborder(c,marginBorderTop,marginBorderLeft,marginBorderBottom,margintBorderRight,
				   titledBorder,paddingBorderTop,paddingBorderLeft,paddingBorderBottom,paddingBorderRight);
	}
	
	//给不包含边框的容器设置带有标题的边框,可以设置标题的水平状态、位置、字体、颜色,并且可以指定内外边距(上左右下都为相同的值)
	public static void addTitledBorder(JComponent c, 
				           int marginBorderSize,
					   String title,
				           int titleJustification,
					   int titlePosition,
					   Font titleFont,
					   Color titleColor,
					   Color bordercolor,
					   int paddingBorderSize) {
		addTitledBorder(c, marginBorderSize, marginBorderSize, marginBorderSize, marginBorderSize, 
				title, titleJustification, titlePosition, titleFont, titleColor, bordercolor, paddingBorderSize, paddingBorderSize, paddingBorderSize, paddingBorderSize);
	}
	
	//给不包含边框的容器设置带有标题的边框,可以设置标题的水平状态、位置、字体、颜色,并且可以指定内外边距(上左右下都为不同的值)
	public static void addTitledBorder(JComponent c, 
		                           int marginBorderTop,
					   int marginBorderLeft,
					   int marginBorderBottom,
					   int margintBorderRight,
					   String title,
					   int titleJustification,
					   int titlePosition,
					   Font titleFont,
					   Color titleColor,
					   Color borderColor,
					   int paddingBorderTop,
					   int paddingBorderLeft,
					   int paddingBorderBottom,
					   int paddingBorderRight) {
		//标题边框
		TitledBorder titledBorder = BorderFactory.createTitledBorder(new LineBorder(borderColor),title,titleJustification,titlePosition,titleFont,titleColor);
		addTitledborder(c,marginBorderTop,marginBorderLeft,marginBorderBottom,margintBorderRight,
				   titledBorder,paddingBorderTop,paddingBorderLeft,paddingBorderBottom,paddingBorderRight);
	}
}

测试类:

package top.jacktgq;
/**
 * 
 * @author 糖果墙
 * @createDate 2019年8月15日 下午9:45:19
 * @description 
 *
 */

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import top.jacktgq.utils.CandyBorderUtils;

public class CandyBorderUtilsDemo extends JFrame {
	public CandyBorderUtilsDemo(String title) {
		super(title);
		Container contentPane = getContentPane();
		JPanel panel = new JPanel();
		contentPane.add(panel);
		panel.setLayout(new FlowLayout());
		panel.add(new JButton("你好"));
		panel.add(new JButton("糖果墙"));
		panel.setBackground(Color.RED);
		//添加30像素的外边距
		//CandyBorderUtils.addMargin(panel, 30);
		//添加30像素的内填充
		//CandyBorderUtils.addPadding(panel, 30);
		//添加20像素的外边距和40像素的内填充
		//CandyBorderUtils.addTitledBorder(panel, 20, "边框工具类测试", 40);
		//添加外边距(10,20,30,40),添加内填充(20,30,40,50)
		/*CandyBorderUtils.addTitledBorder(panel, 10, 20, 30, 40, 
									"边框工具类测试", 20, 30, 40, 50);*/
		//添加20像素的外边距和40像素的内填充,标题居中,处于边框上方,字体微软黑、25号、黄色,边框白色
		/*CandyBorderUtils.addTitledBorder(panel, 20, "边框工具类测试", 
				 TitledBorder.CENTER, TitledBorder.TOP, 
				 new Font("微软雅黑",Font.BOLD,22), 
				 Color.YELLOW, Color.WHITE, 40);*/
		//添加外边距(10,20,30,40),添加内填充(20,30,40,50),标题居中,处于边框上方,字体微软黑、25号、黄色,边框白色
		CandyBorderUtils.addTitledBorder(panel, 10, 20, 30, 40, "边框工具类测试", 
				 TitledBorder.CENTER, TitledBorder.TOP, 
				 new Font("微软雅黑",Font.BOLD,22), 
				 Color.YELLOW, Color.WHITE, 20, 30, 40, 50);
	}
}
运行主类:
package top.jacktgq;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class SwingDemo {
	private static void createGUI() {
		JFrame frame = new CandyBorderUtilsDemo("边框工具类测试");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400,300);
		frame.setVisible(true);
	}
	
	public static void main(String[] args) {
		 SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				createGUI();
			}
		 });
	}
}