效果图
没按压时背景渐变色
鼠标按压时背景色改变
下面是可用的源码
package com.beijin.robot.utils;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;public class ButtonBuilder {private static JButton DEFAULT_BUTTON = new JButton();public static JButton createRadButtonGradientLowBule(){return createRadCustomColorButton(new Color(0x000000),30,null,new Color(0xFFFFFF),new Color(0xF0F7FF),new Color(172,223,253));}/*** 普通圆角自定义颜色按钮,可设置渐变色* @param fgColor 字体前景色,这里默认给的是 new Color(0xF0F7FF);* @param radius 圆角半径 eg:30* @param bgColor 单一背景色 ,注意:如果空则使用渐变色,如果渐变色也空则使用字体前景色* @param bgColorGradient1 背景渐变色一端 (左)* @param bgColorGradient2 背景渐变色另一端 (右)* @param bgPressColor 按钮被按住时的背景色* @return*/public static JButton createRadCustomColorButton(Color fgColor, int radius, Color bgColor,Color bgColorGradient1,Color bgColorGradient2,Color bgPressColor) {JButton button = new RoundButton(radius, bgColor,bgColorGradient1,bgColorGradient2,bgPressColor);//50button.setForeground(fgColor);return button;}/*** 椭圆按钮*/private static class RoundButton extends JButton {private int radius;private Color bgColor;//单一背景颜色,如果有则不用渐变色private Color bgColorGradient1;// 一端背景渐变色1private Color bgColorGradient2;//另一端背景渐变色2private Color bgPressColor;//鼠标按住时的颜色private boolean isPressed = false;public RoundButton(int radius, Color bgColor,Color bgColorGradient1,Color bgColorGradient2,Color bgPressColor) {super();this.radius = radius;this.bgColor = bgColor;this.bgColorGradient1 = bgColorGradient1;this.bgColorGradient2 = bgColorGradient2;this.bgPressColor = bgPressColor;
// super.setBackground(color);setBorder(null);// 取消画矩形super.setContentAreaFilled(false);if(bgPressColor == null){bgPressColor = new Color(0xF0F7FF);}//添加鼠标监听点击事件,记录鼠标按压松开状态,颜色改变需要用到addMouseListener(new MouseAdapter() {@Overridepublic void mousePressed(MouseEvent e) {isPressed = true;}@Overridepublic void mouseReleased(MouseEvent e) {isPressed = false;}});}@Overrideprotected void paintComponent(Graphics g) {Graphics2D g2 = (Graphics2D) g.create();g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);if(bgColor != null){//设置背景色if(isPressed){g2.setColor(bgPressColor);}else{g2.setColor(bgColor);}}else if(bgColorGradient1 != null && bgColorGradient2 != null){if(isPressed){g2.setColor(bgPressColor);}else{//背景色定义渐变色GradientPaint gradient = new GradientPaint(0, 0, bgColorGradient1, super.getSize().width, super.getSize().height, bgColorGradient2, true);// 设置渐变效果g2.setPaint(gradient);}}else{g2.setColor(bgPressColor);}g2.fillRoundRect(0, 0, super.getSize().width - 1, super.getSize().height - 1, radius, radius);super.paintComponent(g2);g2.dispose();}/*** 创建与默认按钮大小相同的 内边距** @param color* @param radius* @return*/public Border createRoundBorder(Color color, int radius) {return new RoundBorder(color, radius);}}/*** 椭圆边框*/private static class RoundBorder implements Border {private Color color;private int radius;public RoundBorder(Color color, int radius) {this.color = color;this.radius = radius;}public Insets getBorderInsets(Component c) {return DEFAULT_BUTTON.getBorder().getBorderInsets(c);};@Overridepublic boolean isBorderOpaque() {return false;}// 实现Border(父类)方法@Overridepublic void paintBorder(Component c, Graphics g, int x, int y, int width,int height) {Graphics2D g2 = (Graphics2D) g.create();g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);g2.setColor(color);g2.drawRoundRect(0, 0, c.getWidth() - 1, c.getHeight() - 1, radius, radius);g2.dispose();}}}