如何能使JButton在调用了setEnable(false) 方法后,按钮不可使用,但图像仍为正常图片,不变灰色? 如题,我给JButton设置了Icon,希望在调用setEnable(false)方法后将其置为失效按钮,但又不希望图像变成灰色,请问大侠们可有办法?不胜感激 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以换个思路,不一定要setEnable(false),考虑将事件代码修改一下,处理后对按钮事件不作处理即可 楼主要的我曾经写过,需要重写ButtonUI我改了重写了三个方法,原因是这三个方法会在enable属性ture和false用不同的色调去画而我直接是以enable属性ture时的色彩去画的关于这个UI的使用方法、你生成一个JButtonJButton 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()); }} 哦 楼主只要图片不变灰啊 那只要重写一个方法就够了也不需要像我上面说的那样烦了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,代码中的clearTextShiftOffset()方法是什么内容?烦请告知,谢了!! clearTextShiftOffset(); 的代码在BasicButtonUI中可以看到是这样的 protected void clearTextShiftOffset(){ this.shiftOffset = 0; }shiftOffset 是一个偏移变量会关系绘制组件时文字的位置 java软件中jcreat能编译但不能运行。什么原因? 关于重写equals()和hashCode()的疑问 如何在SWT中实现 本地打开文件 并显示文件内容?(解决完马上给分) CSDN公告:4月29日凌晨开始强制结帖 关于排列组合算法,但有一定的要求,高手帮忙,谢谢 送分:jdbc的运行为什么出错? 快报!JBuilder 11预览! Stack类问题 ***急救*** 如何在java程序中取得连接的打印机的名称? SCJP的模拟题 ArrayList的迭代器只能向前,不能向后? TableViewer,选中一个字段的某一个值,另一个字段的值自动改变,怎么做?
我改了重写了三个方法,
原因是这三个方法会在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());
}}
那只要重写一个方法就够了
也不需要像我上面说的那样烦了
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);
}
}
}
protected void clearTextShiftOffset(){
this.shiftOffset = 0;
}
shiftOffset 是一个偏移变量
会关系绘制组件时文字的位置