我在我的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()方法。我想知道是否有解决方法。
非常感谢你的帮助。
答案 0 :(得分:1)
我发表了评论,但这值得回答。 添加本机代码,MPlayer,这样你就会陷入impl和操作系统。 getPeer()的弃用主要是b / c,你可以做非常奇怪的东西而不是便携式。
在你的情况下没关系。
旁注: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..
}
祝你好运!
答案 1 :(得分:0)
根据documentation getPeer()已被isDisplayable()取代,但这并不能满足您的需求。显然,与您一样访问同行是违反规范的(更多信息请查看here)。
如果你绝对必须拥有ID,那么你需要另一种方法来掌握它,因为正如我所提到的,getPeer()甚至没有被具有类似功能的某种方法取代,它现在已经有效地变成了“私有”。 / p>