学习Java本机访问时出现com.sun.glass.ui软件包错误 [英] Error with package com.sun.glass.ui while learning Java Native Access

查看:635
本文介绍了学习Java本机访问时出现com.sun.glass.ui软件包错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在JavaFX项目stage.initStyle(StageStyle.UNDECORATED);中使用Undecorated阶段.这是一个模块化的Gradle项目.这也是一个多项目构建,因为我正在努力在IntelliJ中,将其称为多模块构建可能更合适.

I am trying to use an Undecorated stage in a JavaFX project, stage.initStyle(StageStyle.UNDECORATED);. It is a modular Gradle project. It is also a multi-project build, though because I am working on it in IntelliJ, it might be more appropriate to refer to it as a multi-module build.

我希望能够在此Undecorated阶段中添加常规功能.我已经能够添加通常的最小化,还原和关闭按钮,但是如果用户单击Windows任务栏中的程序图标,我也希望它也最小化和还原.

I wish to be able to add regular functionality to this Undecorated stage. I have already been able to add the usual minimize, restore, and close buttons, but if a user clicks on the program's icon in the Windows Task Bar, I want it to minimize and restore as well.

我在此较旧的StackOverflow帖子中找到了可能会执行此操作的代码,但是我遇到了错误,我可以不太清楚.

I have found code in this older StackOverflow post that might do this, but I have run into an error I can't quite figure out.

布莱恩(发帖人)的代码:

brian's (the poster's) Code:

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser;
import static com.sun.jna.platform.win32.WinUser.GWL_STYLE;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class JNATest extends Application {
    public static void main(String[] args) { launch(args); }

    @Override
    public void start(Stage stage) {
        TextArea ta = new TextArea("output\n");
        VBox root = new VBox(5,ta);
        Scene scene = new Scene(root,800,200);
        stage.setTitle("Find this window");
        stage.setScene(scene);
        stage.show();
        //gets this window (stage)
        long lhwnd = com.sun.glass.ui.Window.getWindows().get(0).getNativeWindow();
        Pointer lpVoid = new Pointer(lhwnd);
        //gets the foreground (focused) window
        final User32 user32 = User32.INSTANCE;
        char[] windowText = new char[512];
        HWND hwnd = user32.GetForegroundWindow();
        //see what the title is
        user32.GetWindowText(hwnd, windowText, 512);
        //user32.GetWindowText(new HWND(lpVoid), windowText, 512);//to use the hwnd from stage
        String text=(Native.toString(windowText));
        //see if it's the same pointer
        ta.appendText("HWND java:" + lpVoid + " HWND user32:"+hwnd+" text:"+text+"\n");
        //change the window style if it's the right title
        if (text.equals(stage.getTitle())){
            //the style to change 
            int WS_DLGFRAME = 0x00400000;//s/b long I think
            //not the same constant here??
            ta.appendText("windows api:"+WS_DLGFRAME+" JNA: "+WinUser.SM_CXDLGFRAME);
            int oldStyle = user32.GetWindowLong(hwnd, GWL_STYLE);
            int newStyle = oldStyle & ~0x00400000; //bitwise not WS_DLGFRAME means remove the style
            newStyle = newStyle & ~0x00040000;//WS_THICKFRAME   
            user32.SetWindowLong(hwnd, GWL_STYLE, newStyle);
        }
    }

}

导致我出错的部分是com.sun.glass.ui.Window.getWindows().get(0).getNativeWindow();

错误:

package com.sun.glass.ui is not visible
(package com.sun.glass.ui is declared in module javafx.graphics, which does not export it to module apple.orange.main)

我不太了解这个错误.是因为我尝试在代码中使用sun.glass.ui软件包而在javafx.graphics中声明了该软件包,还是因为它位于javafx.graphics模块内部却无法访问?我最好的猜测是该程序包属于某个模块,我不知道该模块的名称需要作为Gradle中的依赖项,还是作为我的module-info.java文件中的要求包含在内.这是我的能力,但是我的Google搜索结果很少,这说明了此软件包的来源. 此SO问题下的注释表明,此类com.sun软件包完全不应使用,并且似乎暗示有可能会用得更好一些.

I don't really understand this error well. Is the sun.glass.ui package declared in javafx.graphics because I tried to use it in my code or does it reside inside the javafx.graphics module but is not able to be accessed? My best guess is that this package belongs to some module I don't know the name of which I need to include as a dependency in Gradle and as a requires in my module-info.java file. This is something I am capable of doing, but my Google searches have turned up very little which explains where this package comes from. The comments under this SO question suggest that such com.sun packages should not be used at all and seem to imply that there is probably some equivalent better used.

推荐答案

我在Windows 10中遇到了同样的问题.幸运的是,@ Slaw的建议是正确的.

I encountered this same issue in Windows 10. Thankfully, @Slaw 's suggestion was correct.

您需要转到IDE中的VM选项(在IntelliJ中运行->编辑配置...)并添加以下内容:

You need to go to VM options in your IDE (Run -> Edit Configurations..., in IntelliJ) and add this:

--add-exports
javafx.graphics/com.sun.glass.ui=ALL-UNNAMED

在那之后应该可以工作了.

It should work after that.

这篇关于学习Java本机访问时出现com.sun.glass.ui软件包错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆