Java-删除ImageIcon? [英] Java - delete an ImageIcon?

查看:177
本文介绍了Java-删除ImageIcon?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以轻松地从屏幕上完全删除ImageIcon吗?我在任何地方都找不到.

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).

推荐答案

不要重新发明轮子.绘制图标的最简单方法是使用JLabel.该代码将类似于:

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 );

如果您不想再显示图标,则只需在按钮的ActionListener中执行以下操作即可:

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

label.setIcon( null );    

这篇关于Java-删除ImageIcon?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆