Java - delete an ImageIcon?

痴心易碎 提交于 2019-12-13 03:23:26

问题


Is there any way to easily delete an ImageIcon completely from the screen? I can't find one anywhere.

ImageIcon image = new ImageIcon("candle.gif");
image.paintIcon(this, g, 150, 80)

For example, if I wanted to get rid of "image" later on when a button is pressed, what would be the appropriate code? (Not for the button, I know how to do that).


回答1:


Don't reinvent the wheel. The easiest way to paint an Icon is to use a JLabel. The code would be something like:

ImageIcon icon = new ImageIcon(...);
JLabel label = new JLabel( icon );
panel.add( label );

If you don't want to display the icon anymore then in the ActionListener of your button you simple do:

label.setIcon( null );    


来源:https://stackoverflow.com/questions/5914608/java-delete-an-imageicon

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