是否可以通过Java 9获得JavaFX窗口的HWND? [英] Is it somehow possible to get a HWND of a JavaFX window in Java 9?
问题描述
Java 9将限制对私有API的任何访问.这意味着使用反射来检索窗口hwnd的已知方法将不再起作用.
Java 9 will restrict any access to private API. That means that the known methods of retrieving the window hwnd using Reflection won't work anymore.
还有没有办法得到他们?我问是因为我有一个提供用于操作任务栏的API的库(以与
Is there still a way to get them? I ask because I have a library that offers an API for manipulating the Taskbar (in a similar way that Java9 offers). The Java 9 API is still for AWT, so I hope I can get my project setup for Java 9 and JavaFX.
我曾经只调用私有方法,但这将停止工作. 任何解决方案表示赞赏.如果可以使用JNA或BridJ进行本地调用,则可以.
I used to just call the private methods, but this will stop working. Any solution is appreciated. Native calls are ok if they can be done using JNA or BridJ.
推荐答案
一种方法可能是更改已经建议的解决方案到如何我可以在JavaFX上获得JavaFX舞台的窗口句柄(hWnd)吗?在JDK9之前有用:-
One way could be to make changes to an already suggested solution to How can I get the window handle (hWnd) for a Stage in JavaFX? useful prior to JDK9 as :-
try {
TKStage tkStage = stage.impl_getPeer();
Method getPlatformWindow = tkStage.getClass().getDeclaredMethod("getPlatformWindow" );
getPlatformWindow.setAccessible(true);
Object platformWindow = getPlatformWindow.invoke(tkStage);
Method getNativeHandle = platformWindow.getClass().getMethod( "getNativeHandle" );
getNativeHandle.setAccessible(true);
Object nativeHandle = getNativeHandle.invoke(platformWindow);
return new Pointer((Long) nativeHandle);
} catch (Throwable e) {
System.err.println("Error getting Window Pointer");
return null;
}
和module-info.java
有点像:
module your.module {
requires javafx.graphics;
}
但是,由于javax.graphics
将软件包内部导出为特定模块,如下所示:
But since javax.graphics
exports the package internally to specific modules as:
exports com.sun.javafx.tk to
javafx.controls,
javafx.deploy,
javafx.media,
javafx.swing,
javafx.web;
您可以尝试添加编译器选项,以便仍将其用作
You can try adding a compiler option to still make use of it as
--add-exports javax.graphics/com.sun.javafx.tk=your.module
注释/提示: 来自 JEP-261:模块系统 -
必须很好地使用
--add-exports
和--add-opens
选项 关心.您可以使用它们来访问库的内部API 模块,甚至JDK本身,但您需要自担风险:如果 内部API被更改或删除,然后您的库或 应用程序将失败.
The
--add-exports
and--add-opens
options must be used with great care. You can use them to gain access to an internal API of a library module, or even of the JDK itself, but you do so at your own risk: If that internal API is changed or removed then your library or application will fail.
这篇关于是否可以通过Java 9获得JavaFX窗口的HWND?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!