JButton 美化(圆角矩形立体效果加鼠标响应)Demo.rar

上传者: 43647259 | 上传时间: 2021-11-20 15:16:41 | 文件大小: 24KB | 文件类型: -
主要代码:
@Override
public void paintIcon(Component cmp, Graphics g, int x, int y)
{
Color lowerColor = new Color(235,255,235);
Color highColor = new Color(81,184,77);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

// 画一个渐变圆角矩形
RoundRectangle2D.Float r2d=new RoundRectangle2D.Float(0, 0, btnWidth - 1, btnHeight - 1, 20, 20);
Shape clip=g2d.getClip();
g2d.clip(r2d);
GradientPaint paint = new GradientPaint(0.0F,0.0F,lowerColor,0.0F,btnHeight,highColor,true);
g2d.setPaint(paint);
g2d.fillRect(0,0,btnWidth,btnHeight);

g2d.setClip(clip);

// 画一个黑色边框
paint = new GradientPaint(0,0,new Color(0,0,0), 0,btnHeight-1,new Color(100,100,100));
g2d.setPaint(paint);
g2d.drawRoundRect(0,0,btnWidth-1,btnHeight-1,20,20);

// 画一个白色边框
paint=new GradientPaint(0,1,new Color(0,0,0,50),0,btnHeight-3,new Color(255,255,255,100));
g2d.setPaint(paint);
g2d.drawRoundRect(1,1,btnWidth-3,btnHeight-3,18,18);

FontMetrics fm = g2d.getFontMetrics(boldFont);
int textWidth = fm.stringWidth(btnText);
int textAscent = fm.getAscent();

g2d.setColor(hoverColor);
g2d.setFont(boldFont);
g2d.drawString(btnText, (btnWidth - textWidth) / 2, (btnHeight + textAscent) / 2 - 2);
}

文件下载

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明