今天我面临以下问题:
我使用方法setTitle()
设置primaryStage的标题。该标题正确显示在窗口中,但是当我将光标悬停在OS停靠栏中的程序图标上时,我只会看到主程序类的路径(请参见下面的屏幕截图)。我该如何解决?
Title in program window和title in OS dockbar。我使用此代码:
@Override
public void start(Stage stage) throws IOException {
this.stage = stage;
instance = this;
// App initialization
Parent root = FXMLLoader.load(SlimeBot.class.getClassLoader().getResource("fxml/Main.fxml"));
Scene scene = new Scene(root, 800, 600);
stage.setTitle("SlimeBot - Панель управления");
stage.setScene(scene);
stage.show();
}
使用java.awt.Toolkit
中的字段尝试设置标题:
@Getter
private static SlimeBot instance;
static {
try {
Toolkit xToolkit = Toolkit.getDefaultToolkit();
java.lang.reflect.Field awtAppClassNameField = xToolkit.getClass().getDeclaredField("awtAppClassName");
awtAppClassNameField.setAccessible(true);
awtAppClassNameField.set(xToolkit, "SlimeBot - Панель управления");
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
launch(args);
}
@Getter
private Stage stage;
@Override
public void start(Stage stage) throws IOException {
this.stage = stage;
instance = this;
// App initialization
Parent root = FXMLLoader.load(SlimeBot.class.getClassLoader().getResource("fxml/Main.fxml"));
Scene scene = new Scene(root, 800, 600);
stage.setTitle("SlimeBot - Панель управления");
stage.setScene(scene);
stage.show();
}
}```
Okey ..我尝试从您链接上的博客文章中获取代码,但没有任何改变。
@Override
public void start(Stage stage) throws IOException {
this.stage = stage;
instance = this;
// App initialization
Parent root = FXMLLoader.load(SlimeBot.class.getClassLoader().getResource("fxml/Main.fxml"));
Scene scene = new Scene(root, 800, 600);
stage.setTitle("SlimeBot - Панель управления");
try {
Toolkit xToolkit = Toolkit.getDefaultToolkit();
Arrays.asList(xToolkit.getClass().getDeclaredFields()).forEach(s -> System.out.println(s));
java.lang.reflect.Field awtAppClassNameField = xToolkit.getClass().getDeclaredField("awtAppClassName");
awtAppClassNameField.setAccessible(true);
awtAppClassNameField.set(xToolkit, "SlimeBot - Панель управления");
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
stage.setScene(scene);
stage.show();
}
[当我运行JavaFX应用程序时,我从X11收到小警告:
(java:5232): Gdk-WARNING **: 15:35:37.334: XSetErrorHandler() called with a GDK error trap pushed. Don't do that.