How can I customize the render of JRadioButton?

浪子不回头ぞ 提交于 2019-12-11 07:15:26

问题


I've created a JRadioButton subclass in which I override the paintComponent method like so:

@Override
protected void paintComponent(Graphics g) {
    g.drawImage(
        isSelected() ? 
           getCheckedImg() :
           getBasicImg()
    , 0, 0, this);
}

but it seems that once the button is drawn, that's the image it uses forever. The isSelected test doesn't seem to have any effect. Are the graphics cached or something by Java? How do I provide my custom JRadioButton with a selected and unselected image? Do I have to write a custom UI?


回答1:


Read the API. There are methods like:

setIcon()
setSelectedIcon()

among others that you can use instead of doing custom painting.




回答2:


To preserve functionality, it's not hard to extend BasicRadioButtonUI and override the delegate's paint() method. You can install your new UI using setUI().




回答3:


Even in Java swing, I generally override paint rather than paintComponent in order to customize the look. I believe the default paint will call paintComponent, but only if the component must be repainted.



来源:https://stackoverflow.com/questions/3322733/how-can-i-customize-the-render-of-jradiobutton

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!