如题,我给JButton设置了Icon,希望在调用setEnable(false)方法后将其置为失效按钮,但又不希望图像变成灰色,请问大侠们可有办法?不胜感激 

解决方案 »

  1.   

    可以换个思路,不一定要setEnable(false),考虑将事件代码修改一下,处理后对按钮事件不作处理即可
      

  2.   

    楼主要的我曾经写过,需要重写ButtonUI
    我改了重写了三个方法,
    原因是这三个方法会在enable属性ture和false用不同的色调去画
    而我直接是以enable属性ture时的色彩去画的
    关于这个UI的使用方法、你生成一个JButton
    JButton btn = new JButton();
    btn.setUI(new MyButtonUI());
    这样就行了,
    MyButtonUI中用到了MetalUtils类中的部分方法
    由于这个类是一个非公有类,不能在你的工程中直接引用
    所以你可能要自己处理一下
    在MyButtonUI的同级目录下自己建一个MetalUtils类
    然后把javax.swing.plaf.metal.MetalUtils的源代码copy过来,
    (可能需要,添加一句 import javax.swing.plaf.metal.MetalLookAndFeel;)
    (源码在JDK安装目录中可以找到)
    本来是想把这个类直接写在MyButtonUI中的,但是源码实在太长,只好让你自己处理一下了
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Rectangle;import javax.swing.AbstractButton;
    import javax.swing.ButtonModel;
    import javax.swing.Icon;
    import javax.swing.JComponent;
    import javax.swing.plaf.UIResource;
    import javax.swing.plaf.basic.BasicButtonUI;import sun.swing.SwingUtilities2;public class MyButtonUI extends BasicButtonUI {    public void update(Graphics g, JComponent c) {
            AbstractButton button = (AbstractButton)c;
            if ((c.getBackground() instanceof UIResource) &&
                      button.isContentAreaFilled()) {
                ButtonModel model = button.getModel();
                if (MetalUtils.isToolBarButton(c)) {
                    if (!model.isArmed() && !model.isPressed() &&
                            MetalUtils.drawGradient(
                            c, g, "Button.gradient", 0, 0, c.getWidth(),
                            c.getHeight(), true)) {
                        paint(g, c);
                        return;
                    }
                }
                else if (MetalUtils.drawGradient(
                            c, g, "Button.gradient", 0, 0, c.getWidth(),
                            c.getHeight(), true)) {
                    paint(g, c);
                    return;
                }
            }
            super.update(g, c);
        }    protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect){
            AbstractButton b = (AbstractButton) c;
            ButtonModel model = b.getModel();
            Icon icon = b.getIcon();
            Icon tmpIcon = null;
            if(icon == null) {
                return;
            }
            Icon selectedIcon = null;
            if (model.isSelected()) {
                selectedIcon = (Icon) b.getSelectedIcon();
                if (selectedIcon != null) {
                    icon = selectedIcon;
                }
            }
            if(model.isPressed() && model.isArmed()) {
                tmpIcon = (Icon) b.getPressedIcon();
                if(tmpIcon != null) {
                    clearTextShiftOffset();
                }
            } else if(b.isRolloverEnabled() && model.isRollover()) {
                if(model.isSelected()) {
                    tmpIcon = (Icon) b.getRolloverSelectedIcon();
                    if (tmpIcon == null) {
                        tmpIcon = selectedIcon;
                    }
                }
                if (tmpIcon == null) {
                    tmpIcon = (Icon) b.getRolloverIcon();
                }
            }
            if(tmpIcon != null) {
                icon = tmpIcon;
            }
            if(model.isPressed() && model.isArmed()) {
                icon.paintIcon(c, g, iconRect.x + getTextShiftOffset(),
                        iconRect.y + getTextShiftOffset());
            } else {
                icon.paintIcon(c, g, iconRect.x, iconRect.y);
            }
        }    protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
            AbstractButton b = (AbstractButton) c;
            FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
            int mnemIndex = b.getDisplayedMnemonicIndex();
            g.setColor(b.getForeground());
            SwingUtilities2.drawStringUnderlineCharAt(c, g,text,mnemIndex,
                    textRect.x, textRect.y + fm.getAscent());
        }}
      

  3.   

    哦 楼主只要图片不变灰啊 
    那只要重写一个方法就够了
    也不需要像我上面说的那样烦了
    import java.awt.Graphics;
    import java.awt.Rectangle;import javax.swing.AbstractButton;
    import javax.swing.ButtonModel;
    import javax.swing.Icon;
    import javax.swing.JComponent;
    import javax.swing.plaf.basic.BasicButtonUI;public class MyButtonUI extends BasicButtonUI {    protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect){
    AbstractButton b = (AbstractButton) c;
    ButtonModel model = b.getModel();
    Icon icon = b.getIcon();
    Icon tmpIcon = null;
    if(icon == null) {
    return;
    }
    Icon selectedIcon = null;
    if (model.isSelected()) {
    selectedIcon = (Icon) b.getSelectedIcon();
    if (selectedIcon != null) {
    icon = selectedIcon;
    }
    }
    if(model.isPressed() && model.isArmed()) {
    tmpIcon = (Icon) b.getPressedIcon();
    if(tmpIcon != null) {
    clearTextShiftOffset();
    }
    } else if(b.isRolloverEnabled() && model.isRollover()) {
    if(model.isSelected()) {
    tmpIcon = (Icon) b.getRolloverSelectedIcon();
    if (tmpIcon == null) {
    tmpIcon = selectedIcon;
    }
    }
    if (tmpIcon == null) {
    tmpIcon = (Icon) b.getRolloverIcon();
    }
    }
    if(tmpIcon != null) {
    icon = tmpIcon;
    }
    if(model.isPressed() && model.isArmed()) {
    icon.paintIcon(c, g, iconRect.x + getTextShiftOffset(),
    iconRect.y + getTextShiftOffset());
    } else {
    icon.paintIcon(c, g, iconRect.x, iconRect.y);
    }
    }
    }
      

  4.   

    请问一下sunyiz,代码中的clearTextShiftOffset()方法是什么内容?烦请告知,谢了!!
      

  5.   

    clearTextShiftOffset(); 的代码在BasicButtonUI中可以看到是这样的
        protected void clearTextShiftOffset(){
            this.shiftOffset = 0;
        }
    shiftOffset 是一个偏移变量
    会关系绘制组件时文字的位置