慕的地8271018
我遇到了同样的问题,并通过使用 Windows API 对窗口进行子类化来解决它。如果你被允许使用 JNA,下面的代码会对你有所帮助。我的用户32.javaimport com.sun.jna.Callback;import com.sun.jna.Native;import com.sun.jna.platform.win32.*;import com.sun.jna.win32.W32APIOptions;public interface MyUser32 extends User32 { int WM_MENUCHAR = 0x0120; int MNC_CLOSE = 1; int VK_RETURN = 0x0d; MyUser32 INSTANCE = Native.loadLibrary("user32", MyUser32.class, W32APIOptions.UNICODE_OPTIONS); LONG_PTR SetWindowLongPtr(WinDef.HWND hWnd, int nIndex, Callback callback); LRESULT CallWindowProc(LONG_PTR lpPrevWndProc, HWND hWnd, int Msg, WPARAM wParam, LPARAM lParam);}App.javaimport com.sun.jna.Pointer;import com.sun.jna.platform.win32.*;import com.sun.jna.platform.win32.WinDef.*;import com.sun.jna.win32.StdCallLibrary;import javafx.application.Application;import javafx.stage.Stage;import java.lang.reflect.Method;public class App extends Application implements StdCallLibrary.StdCallCallback { private BaseTSD.LONG_PTR baseWndProc; public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { // set up scene ... primaryStage.show(); final HWND hWnd = new HWND(getWindowPointer(primaryStage)); baseWndProc = MyUser32.INSTANCE.SetWindowLongPtr(hWnd, User32.GWL_WNDPROC, this); } public LRESULT callback(HWND hWnd, int Msg, WPARAM wParam, LPARAM lParam) { if (Msg == MyUser32.WM_MENUCHAR && (wParam.longValue() & 0xffff) == MyUser32.VK_RETURN) { return new LRESULT(MyUser32.MNC_CLOSE << 16); } return MyUser32.INSTANCE.CallWindowProc(baseWndProc, hWnd, Msg, wParam, lParam); } private Pointer getWindowPointer(Stage stage) { try { Method getPeer = stage.getClass().getMethod("impl_getPeer"); final Object tkStage = getPeer.invoke(stage); Method getPlatformWindow = tkStage.getClass().getDeclaredMethod("getPlatformWindow"); getPlatformWindow.setAccessible(true); final Object platformWindow = getPlatformWindow.invoke(tkStage); Method getNativeHandle = platformWindow.getClass().getMethod("getNativeHandle"); return new Pointer((Long) getNativeHandle.invoke(platformWindow)); } catch (Throwable t) { return null; } }