如何获取 Java 组件 ID

发布于 2024-10-21 00:16:04 字数 508 浏览 2 评论 0原文


我在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囚你心 2024-10-28 00:16:04

我发表了评论,但这值得一个答案。
添加本机代码、MPlayer,因此您将陷入 impl 和操作系统的困境。 getPeer() 的弃用主要是因为你可以用它做一些非常奇怪的事情并且不可移植。

在你的情况下,这并不重要。

附带说明:WComponentPeer 有一个公共 getHWnd() 方法,因此您不需要通过反射来欺骗它。您现在拥有的代码实际上非常不安全,因为它不检查实际的对等点。

你可以这样替换它:

long hWnd = 0
try{ 
  Class clazz = Class.forName("sun.awt.windows.WComponentPeer);
  synchronized(overlay.getTreeLock()){
    ComponentPeer peer = overlay.getPeer();
    if (clazz.isInstance(peer)){
      hWnd = ((sun.awt.windows.WComponentPeer) overlay.getPeer()).getHWnd();
    }
  }
}catch(ClassNotFound _noWindows){
//process..
}

祝你好运!

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:

long hWnd = 0
try{ 
  Class clazz = Class.forName("sun.awt.windows.WComponentPeer);
  synchronized(overlay.getTreeLock()){
    ComponentPeer peer = overlay.getPeer();
    if (clazz.isInstance(peer)){
      hWnd = ((sun.awt.windows.WComponentPeer) overlay.getPeer()).getHWnd();
    }
  }
}catch(ClassNotFound _noWindows){
//process..
}

good luck!

妄想挽回 2024-10-28 00:16:04

根据 文档< /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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文