如何获取 Java 组件 ID
我在我的 java 应用程序中使用 MPlayer。根据它的文档,我需要告诉 MPlayer 窗口 ID 来嵌入它。我这样做是这样的:
long winid = 0; //Window ID.
if (osName.startsWith("Windows")){
final Class<?> cl = Class.forName("sun.awt.windows.WComponentPeer");
java.lang.reflect.Field f = cl.getDeclaredField("hwnd");
f.setAccessible(true);
winid = f.getLong(overlay.getPeer()); //overlay is a canvas where MPlayer is embedded.
}
System.out.println(winid);
但是, getPeer() 方法已被弃用。我想知道是否有解决方法。
非常感谢您的帮助。
I'm using MPlayer in my java application. According to its doc, it's needed that I tell to MPlayer the window ID for embedding it. I'm doing this like that:
long winid = 0; //Window ID.
if (osName.startsWith("Windows")){
final Class<?> cl = Class.forName("sun.awt.windows.WComponentPeer");
java.lang.reflect.Field f = cl.getDeclaredField("hwnd");
f.setAccessible(true);
winid = f.getLong(overlay.getPeer()); //overlay is a canvas where MPlayer is embedded.
}
System.out.println(winid);
However, the getPeer() method is deprecated. I would want to know if there's a workaround for it.
Thanks a lot for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发表了评论,但这值得一个答案。
添加本机代码、MPlayer,因此您将陷入 impl 和操作系统的困境。 getPeer() 的弃用主要是因为你可以用它做一些非常奇怪的事情并且不可移植。
在你的情况下,这并不重要。
附带说明:WComponentPeer 有一个公共 getHWnd() 方法,因此您不需要通过反射来欺骗它。您现在拥有的代码实际上非常不安全,因为它不检查实际的对等点。
你可以这样替换它:
祝你好运!
I dropped a comment but that deserves an answer.
Adding native code, MPlayer, so you are stuck with the impl and the OS. The deprecation of getPeer() is mostly b/c you can do really weird stuff with and not portable.
In your case it doesn't matter.
On a side note: WComponentPeer has a public getHWnd() method, so you do not need to trick it via reflection. The code you have now is actually pretty unsafe since it doesn't check for the actual peer.
you can replace it like that:
good luck!
根据 文档< /a> getPeer() 已被 isDisplayable() 取代,但这不会给你你所需要的。显然,像您这样访问同级是违反规范的(看看
如果您绝对必须拥有 ID,那么您需要另一种方法来获取它,因为正如我提到的 getPeer() 甚至还没有被具有类似功能的某些方法所取代,它现在实际上已经变成了“私有”。
According to the documentation getPeer() has been replaced by isDisplayable() but this won't give you what you need. Apparently it is a violation of the spec to be accessing peers like you are (have a look here for more info).
If you absolutely must have the ID then you need another way to get hold of it because as I mentioned getPeer() has not even been replaced by some method that has similar functionality, it has now effectively become "private".