Java how and when exactly is the paint() method called?

后端 未结 1 1478
感动是毒
感动是毒 2021-01-27 09:41

I have been told many times that the paint() method will be called as and when required when I extend my class to JFrame but for eg. in the code the paint method is not being ca

相关标签:
1条回答
  • 2021-01-27 10:23

    You have two frames:

    1. You extend a JFrame and override the paint() method, but that frame is never made visible so the paint() method is never invoked.

    2. Then you create a new JFrame which you make visible, but this frame has no custom painting so you just see the frame.

    In any case this is NOT the way to do custom painting. Custom painting is done by overriding paintCompnent(...) of a JPanel and then you add the panel to the frame. Read the section from the Swing tutorial on Custom Painting for more information and working examples that you can customize.

    The tutorial example will show you a better way to create your class so there is no need to extend a JFrame. Follow the tutorial example.

    0 讨论(0)
提交回复
热议问题