自定义JCheckBox中的“框”

时间:2018-12-30 14:05:48

标签: java swing jcheckbox

我想更改 JCheckBox Box 的外观。我尝试创建一个“ CustomIcon” class ,该类 implemenst “ Icon” ,并使用方法 “ JCheckBox.setDisabledIcon()” “ JCHeckBox.setDisabledSelectedIcon()” 我班上的图标,但没有得到任何结果。这是尝试 @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)));

您能帮我发现错误,或向我解释如何做吗?

1 个答案:

答案 0 :(得分:1)

我将setDisabledSelectedIcon更改为setSelectedIcon,将setDisabledIcon更改为setIcon,即使没有禁用复选框,它也可以正常工作。