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

右领军大都督 2010-11-22 07:04:15
如题,我给JButton设置了Icon,希望在调用setEnable(false)方法后将其置为失效按钮,但又不希望图像变成灰色,请问大侠们可有办法?不胜感激
...全文
670 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),考虑将事件代码修改一下,处理后对按钮事件不作处理即可
针对矿山运输车辆超载监管中长期存在的源头管控缺失、数据孤岛严重、人工执法覆盖面有限等突出问题,本方案构建了一套以矿山企业侧地磅和过车数据为核心数据源的智能监管体系。方案采用"感知层—传输层—平台层—应用层"四层总体架构,在矿山企业出入口部署地磅称重系统、车牌识别摄像机、视频监控及GPS/北斗电子围栏等感知设备,通过光纤专线与5G无线相结合的传输网络,将称重数据、过车图片、视频流和定位轨迹实时汇聚至数据中台。平台层基于PostgreSQL、TDengine、MinIO等多元存储和Flink流式计算引擎,实现数据接入、治理、计算与服务全链路支撑。应用层面向交警和运管部门,提供实时监控中心、超载智能三级预警、车辆轨迹追踪、多维数据分析、执法联动电子证据包生成、企业信用A/B/C/D四级评价等功能,并与交警六合一、运管综合执法、治超非现场执法等系统深度对接,打通跨部门数据壁垒。方案遵循源头治理、数据驱动、部门协同、安全可靠、适度超前、分步实施六大原则,按"一期试点—二期推广—三期深化"分步推进,建设周期12至14个月。预期实现超载检出率从20%提升至90%、执法响应从小时级缩短至分钟级、监管覆盖率100%,同时减少道路维修费用约30%、降低人工执法成本50%,为道路交通安全治理现代化提供可落地的技术路径。

62,622

社区成员

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

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