longyi_java 发表于 2013-1-27 04:35:54

修改Button的背景颜色

在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;int spread2 = Theme.buttonSpreadDark;if(!b.isEnabled()) {//按钮不可用spread1 = Theme.buttonSpreadLightDisabled;spread2 = Theme.buttonSpreadDarkDisabled;}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.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.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());} 
页: [1]
查看完整版本: 修改Button的背景颜色