六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 139|回复: 0

修改Button的背景颜色

[复制链接]

升级  39.33%

29

主题

29

主题

29

主题

秀才

Rank: 2

积分
109
 楼主| 发表于 2013-1-27 04:35:54 | 显示全部楼层 |阅读模式
在ButtionUI的方法public void paint(Graphics g, JComponent c)中调用drawXpButton(),可以设置Button的背景颜色
 
 
private void drawXpButton(Graphics g, AbstractButton b, Color c, boolean isRollover) {if(!b.isContentAreaFilled()) return;if(!b.isOpaque()) return;//不完成透明int w = b.getWidth();//Button的宽度int h = b.getHeight();//Button的高度// paint border background 绘制边框Color bg = b.getParent().getBackground();g.setColor(bg);                //g.drawLine(int x1, int y1, int x2, int y2)                 //在此图形上下文的坐标系统中,使用当前颜色在点 (x1, y1) 和 (x2, y2) 之间画一条线。                //g.drawRect(0, 0, w - 1, h - 1) 是在左上对角与右下对角之间画一条连线g.drawRect(0, 0, w - 1, h - 1);int spread1 = Theme.buttonSpreadLight[Theme.style];int spread2 = Theme.buttonSpreadDark[Theme.style];if(!b.isEnabled()) {//按钮不可用spread1 = Theme.buttonSpreadLightDisabled[Theme.style];spread2 = Theme.buttonSpreadDarkDisabled[Theme.style];}float spreadStep1 = 10.0f * spread1 / (h - 3);float spreadStep2 = 10.0f * spread2 / (h - 3);int halfY = h / 2;int yd;for (int y = 1; y < h - 1; y++) {if(y < halfY) {yd = halfY - y;g.setColor(ColorRoutines.lighten(c, (int)(yd * spreadStep1)));}else if(y == halfY) {g.setColor(c);}else {yd = y - halfY;g.setColor(ColorRoutines.darken(c, (int)(yd * spreadStep2)));}g.drawLine(2, y, w - 3, y);if(y == 1) {// left vertical line 画左垂直的线g.drawLine(1, 1, 1, h - 2);if(isRollover || isDefault) {// right vertical line 画右垂直的线g.drawLine(w - 2, 1, w - 2, h - 2);}}else if(y == h - 2 && !isRollover && !isDefault) {// right vertical line 画右垂直的线g.drawLine(w - 2, 1, w - 2, h - 2);}}// 1 pixel away from each cornerif(isRollover) {//指示鼠标在按钮上                    //获取指示鼠标在按钮上的颜色g.setColor(Theme.buttonRolloverColor[Theme.style].getColor());g.drawLine(1, h - 2, 1, h - 2);//绘制左下角点g.drawLine(w - 2, h - 2, w - 2, h - 2);//绘制右上角点}else if(isDefault) {                    //获取默认的颜色g.setColor(Theme.buttonDefaultColor[Theme.style].getColor());g.drawLine(1, h - 2, 1, h - 2);//绘制左下角点g.drawLine(w - 2, h - 2, w - 2, h - 2);//绘制右上角点}}public static Color lighten(Color c, int amount) {if (amount < 0) return c;if (amount > 100) amount = 100;int dr = (int)Math.round((255 - c.getRed()) * amount / 100.0);int dg = (int)Math.round((255 - c.getGreen()) * amount / 100.0);int db = (int)Math.round((255 - c.getBlue()) * amount / 100.0);return new Color(c.getRed() + dr, c.getGreen() + dg, c.getBlue() + db, c.getAlpha());}public static Color darken(Color c, int amount) {if (amount < 0 || amount > 100) return c;int r = (int)Math.round(c.getRed() * (100 - amount) / 100.0);int g = (int)Math.round(c.getGreen() * (100 - amount) / 100.0);int b = (int)Math.round(c.getBlue() * (100 - amount) / 100.0);return new Color(r, g, b, c.getAlpha());} 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表