java 什么是 addNotify();?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15519916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is addNotify();?
提问by Space Ghost
I've tried to find a laymans definition of addNotify()
but I can't really get any answer using Google.
我试图找到一个外行的定义,addNotify()
但我无法使用谷歌真正得到任何答案。
As far as I know, when overriding addNotify()
in my class, I should call super.addNotify();
and then do whatever else afterward.
据我所知,addNotify()
在我的班级中覆盖时,我应该打电话super.addNotify();
,然后再做其他事情。
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?
我的问题是,是否addNotify()
自动运行?它的目的是什么,当我覆盖它时会发生什么,此外,我为什么要覆盖这个方法?
Thank's.
谢谢。
采纳答案by Stephen C
My question is, does addNotify() run automatically?
我的问题是,addNotify() 会自动运行吗?
Yes. The precise where and when depends on the internals of the AWT implementation.
是的。确切的地点和时间取决于 AWT 实现的内部结构。
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.)
它如javadoc中所述。它是非常低级的东西,是将 AWT 世界连接到本机窗口世界的“胶水”的一部分。(我故意说得过于高深和含糊。如果您想了解详细信息,请下载并阅读 OpenJDK 源代码。)
and what happens when I override it
当我覆盖它时会发生什么
You'd probably break AWT :-)
你可能会破坏 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.
我想不出这样做的好理由……除非您试图将 AWT 移植到不同的操作系统或不同的本机窗口系统。
回答by PermGenError
Component#addNotify()is a method in java.awt.Component
class. The purpose of this method as stated in the API:
Component#addNotify()是java.awt.Component
类中的一个方法。该方法的目的如 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.
通过将其连接到本机屏幕资源,使该组件可显示。该方法由工具包内部调用,不应由程序直接调用。
回答by Shreyos Adikari
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.
方法addNotify() / removeNotify()
是 AWT 提供的唯一钩子,我们可以在其中正确销毁和重新创建 OpenGL 上下文,前提是底层的本机小部件正在被销毁。addNotify()
低级组件使用它与操作系统上的底层对等点进行交互,以便真正发生某些事情,而不仅仅是屏幕上的漂亮图片。
最好避免使用这种方法。