如何能使JButton在调用了setEnable(false) 方法后,按钮不可使用,但图像仍为正常图片,不变灰色?

右领军大都督 2010-11-22 07:04:15
如题,我给JButton设置了Icon,希望在调用setEnable(false)方法后将其置为失效按钮,但又不希望图像变成灰色,请问大侠们可有办法?不胜感激
...全文
606 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunyiz 2010-11-23
  • 打赏
  • 举报
回复
clearTextShiftOffset(); 的代码在BasicButtonUI中可以看到

是这样的
    protected void clearTextShiftOffset(){
this.shiftOffset = 0;
}


shiftOffset 是一个偏移变量
会关系绘制组件时文字的位置
右领军大都督 2010-11-23
  • 打赏
  • 举报
回复
请问一下sunyiz,代码中的clearTextShiftOffset()方法是什么内容?烦请告知,谢了!!
sam012 2010-11-22
  • 打赏
  • 举报
回复
学习了,谢谢楼上分享!
sunyiz 2010-11-22
  • 打赏
  • 举报
回复
哦 楼主只要图片不变灰啊
那只要重写一个方法就够了
也不需要像我上面说的那样烦了
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);
}
}
}
sunyiz 2010-11-22
  • 打赏
  • 举报
回复
楼主要的我曾经写过,需要重写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());
}

}

约翰羊 2010-11-22
  • 打赏
  • 举报
回复
1楼说的在理。
buqitianxie 2010-11-22
  • 打赏
  • 举报
回复
可以换个思路,不一定要setEnable(false),考虑将事件代码修改一下,处理后对按钮事件不作处理即可

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧