Clear foreground of a drawing panel in Swing

北城以北 提交于 2019-12-23 19:46:44

问题


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.


回答1:


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.




回答2:


The converse of @mre's answer is that "It is up to the look and feel to honor this property, some may choose to ignore it." I don't know of a PanelUI that ignores the foreground color, but it's something to be aware of when testing.

Addendum: As @kleopatra comments, the JComponent@setForeground() API links to the Component#getForeground() API, which notes the following: "if this component does not have a foreground color, the foreground color of its parent is returned."



来源:https://stackoverflow.com/questions/12158538/clear-foreground-of-a-drawing-panel-in-swing

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