油漆()和油漆组件()之间的区别?

问题描述:

我已经尝试了这方面的教程,但我仍然不太明白.基本上我的问题是哪种方法更好,为什么?我应该使用 paint 还是 paintComponent?

I have tried tutorials on this but I still don't quite understand it. Basically my question is which method is better and why? Should I use paint or paintComponent?

请尽量保持答案简单,谢谢.

Please try to keep the answer simple, thanks.

引用自 paint() 方法的文档

该方法实际上将绘制工作委托给三个受保护的方法:paintComponent、paintBorder 和paintChildren....一个只想专门化 UI(外观和感觉)委托的绘制方法的子类应该只覆盖paintComponent.

This method actually delegates the work of painting to three protected methods: paintComponent, paintBorder, and paintChildren. ... A subclass that just wants to specialize the UI (look and feel) delegate's paint method should just override paintComponent.

看起来 paint() 方法实际上绘制了组件,包括边框和子组件.如果您只想自定义不包括边框和子组件的外观,您可以使用 paintComponent().

It looks like the paint() method actually draws the component, including the border and children. If you only want to customize the component's appearance excluding the border and children, you use paintComponent().

http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#paint(java.awt.Graphics)