What is addNotify();?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-10 05:01:17

问题


I've tried to find a laymans definition of addNotify() but I can't really get any answer using Google.

As far as I know, when overriding addNotify() in my class, I should call super.addNotify(); and then do whatever else afterward.

My question is, does addNotify() run automatically? What is it's purpose and what happens when I override it and furthermore, why would I ever want to override this method?

Thank's.


回答1:


My question is, does addNotify() run automatically?

Yes. The precise where and when depends on the internals of the AWT implementation.

What is it's purpose

It is as described in the javadoc. It is very low level stuff that is part of the "glue" that connects the AWT world to the native windowing world. (I'm being deliberately high-level and vague. If you want the nitty-gritty details, download and read the OpenJDK source code.)

and what happens when I override it

You'd probably break AWT :-)

and furthermore, why would I ever want to override this method?

I can't think of a good reason to do this ... unless you were trying to port AWT to a different operating system or a different native windowing system.




回答2:


Component#addNotify() is a method in java.awt.Component class. The purpose of this method as stated in the API:

Makes this Component displayable by connecting it to a native screen resource. This method is called internally by the toolkit and should not be called directly by programs.




回答3:


Methods addNotify() / removeNotify() are the only hooks the AWT provides in which we can correctly destroy and recreate the OpenGL context, given that the underlying native widget is being destroyed.
addNotify() is used by low level components to interact with the underlying peer on the operating system so that something REALLY happens, not just pretty pictures on a screen.
Better to avoid using this method.



来源:https://stackoverflow.com/questions/15519916/what-is-addnotify

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