在Swing中清楚绘图面板的前景 [英] Clear foreground of a drawing panel in Swing

查看:133
本文介绍了在Swing中清楚绘图面板的前景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

制作这个画笔程序我决定要清除面板容器( panel2 在链接代码中)。我使用了 PanelName.setForeground(null),它工作。我想知道的是,是否存在此方法的已知缺陷以及从容器中移除图形对象的其他方式。

解决方案

<如果你重载 paintComponent ,你可以使用 Graphics 。 oracle.com/javase/7/docs/api/java/awt/Graphics.html#clearRect%28int,%20int,%20int,%20int%29\">Graphics#clearRect 。但总的来说,我更喜欢你的方法,因为我会要求你继承 JPanel ,或者匿名声明一个。

Making this paintbrush program I decided I want to clear the panel container (panel2 in the linked code). I used PanelName.setForeground(null) and it worked. What I'd like to know is if there are known drawbacks of this approach and other ways of removing graphics objects from the container.

解决方案

If you override paintComponent, you can clear its Graphics using Graphics#clearRect. But overall, I prefer your approach because mine would require you to either subclass JPanel, or anonymously declare one.

这篇关于在Swing中清楚绘图面板的前景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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