我發現在互聯網上的解決方案,並改變了它自己。
class CheckBoxIcon implements Icon {
public void paintIcon(Component component, Graphics g, int x, int y) {
AbstractButton abstractButton = (AbstractButton)component;
ButtonModel buttonModel = abstractButton.getModel();
if(buttonModel.isSelected())
g.drawImage(createImage("resources/checkbox2.png"), x, y, component);
else
g.drawImage(createImage("resources/checkbox1.png"), x, y, component);
}
public int getIconWidth() {
return 13;
}
public int getIconHeight() {
return 13;
}
protected Image createImage(String path) {
URL imageURL = CheckBoxIcon.class.getResource(path);
Image icn = null;
if (imageURL == null) {
if(null==icn){
//System.out.println("path: "+path);
icn = new ImageIcon (MyGUI.class.getResource(path)).getImage();
if(null!=icn)
return icn;
else{
System.err.println("Resource not found: " + path);
return null;
}
}
return null;
} else {
return (new ImageIcon(imageURL)).getImage();
}
}
}
然後在代碼中使用這一行。
UIManager.put("CheckBox.icon", new CheckBoxIcon());
[修改外觀](http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/index.html)? – MadProgrammer