我想更改 JCheckBox 内 Box 的外观。我尝试创建一个“CustomIcon”类,它实现“图标”,并且,使用方法“JCheckBox.setDisabledIcon()”和“JCHeckBox.setDisabledSelectedIcon()”设置图标与我的类(class),但我没有得到任何结果。这是我在尝试 @Override "BasicCheckBoxUI.paint()" 方法后找到的最佳解决方案,但它也不起作用.
CustomIcon 类:
{
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.Icon;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Ionut Cicio
*/
public class CustomizedIcon implements Icon{
public int width;
public int height;
public Color color;
public CustomizedIcon(int width, int height, Color color){
this.width = width;
this.height = height;
this.color = color;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y){
g.setColor(this.color);
g.fillRect(x, y, width, height);
g.drawRect(x, y, width, height);
}
@Override
public int getIconWidth(){
return this.width;
}
@Override
public int getIconHeight(){
return this.height;
}
}
利用率:
rememberPasswordCheckBox.setDisabledSelectedIcon(new CustomizedIcon(10, 10, new Color(100, 255, 100)));
rememberPasswordCheckBox.setDisabledIcon(new CustomizedIcon(10, 10, new Color(255, 100, 100)));
您能帮我找出错误,或者解释一下该怎么做吗?
最佳答案
我将 setDisabledSelectedIcon 更改为 setSelectedIcon,将 setDisabledIcon 更改为 setIcon,它工作得很好 - 即使对于禁用的复选框也是如此。
关于java - 在 JCheckBox 中自定义 "Box",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53978263/