Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

java - Difference between paint() and 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Quoting from documentation of paint() method

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.

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...