如何为javafx应用程序设置图标?

问题描述 投票:0回答:1

如何为javafx应用程序设置图标?

当我向项目添加图标时,出现错误。

我正在 JavaFX 中开发一个应用程序,我正在尝试根据下面的代码更改我的应用程序的图标:

我的代码:

public class DEMO extends Application {



@Override
public void start(Stage stage) throws Exception {
    //Parent root = FXMLLoader.load(getClass().getResource("MainScreen.fxml"));
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("MainScreen.fxml"));
    Parent root = (Parent) fxmlLoader.load();
    MainScreenController mainScreenController = (MainScreenController) fxmlLoader.getController();
    mainScreenController.setMainScreenController(mainScreenController);
 
    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.setTitle("demo");
    stage.getIcons().add(new Image("/icon.png"));
    stage.setOnCloseRequest(e -> {
        Platform.exit();
        System.exit(0);
    });
    stage.show();
}




public static void main(String[] args) {
    
    launch(args);

}

}

当我运行时出现错误。

Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at 
  java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at 
  javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at  java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
 Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at 
 javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:832)
 Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1107)
at javafx.graphics/javafx.scene.image.Image.<init>(Image.java:617)
at remotecontrolpc.RemoteControlPC.start(RemoteControlPC.java:37)
at 
  javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at 
 javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
  Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1099)
... 11 more
  Exception running application remotecontrolpc.RemoteControlPC

  Process finished with exit code 1 

为什么会出现这个错误?

如何解决这个错误?

javafx
1个回答
0
投票

这对我有用:

import javafx.scene.image.Image;
[....]

primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/icons/myimage-icon.jpg")))

其中“icons”是我的类的类路径根目录。

© www.soinside.com 2019 - 2024. All rights reserved.