在Gnome顶部栏中设置Java Swing应用程序标题的正确方法是什么?

Dav*_*e F 5 java swing gnome-3

我正在使用以下代码在Gnome 3的顶部栏中设置我的Java Swing应用程序的标题.但是,当我运行它时,我会收到代码下方显示的警告.有没有更好的方法在代码中设置应用程序标题?请注意,这不是关于设置窗口标题本身的问题.

try
{
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Field awtAppClassNameField = toolkit.getClass().getDeclaredField("awtAppClassName");
    awtAppClassNameField.setAccessible(true);
    awtAppClassNameField.set(toolkit, "FNDice");
}
catch (NoSuchFieldException | IllegalAccessException e)
{
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

以下是我在运行应用程序时看到的警告.

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.gmail.fishnet37222.fndice.App (file:/home/dave/IdeaProjects/fndice/target/classes/) to field sun.awt.X11.XToolkit.awtAppClassName
WARNING: Please consider reporting this to the maintainers of com.gmail.fishnet37222.fndice.App
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Run Code Online (Sandbox Code Playgroud)

Edw*_*uck 0

这是两种不同事物的副作用,通过反射使用非 API 接口,以及使用使用模块加载系统的新 JVM(在 Java 9 中引入)

借助 Java 模块,JAR 文件现在包含更多有关其他 JAR 文件应访问所包含代码的哪些部分的元数据。这使得许多以前只能通过 OSGI 或自定义类加载器才能实现的事情成为可能。

可能性是您正在使用的代码和您正在使用的库尚未更新以正确工作/与模块系统交互,这是默认情况下强制执行的。要让遗留反射代码以向后兼容的方式访问非 API 入口点,您需要将命令行标志添加--illegal-access=permit到 java 启动命令中。

请注意,如果您同时执行运行时启动和编译器启动,则可能需要执行此操作。

出于好奇,在这个平台上不起作用jframe.setTitle("title"),或者之前的代码库setTitle可用吗?