简体   繁体   中英

Error trying to change JavaFX application icon

I am developing an application in JavaFX and I am trying to change the icon of my application, according to the code below:

public class Login extends Application 

{

Image applicationIcon = new Image("/src/Icons/message.png");

@Override
public void start(Stage primaryStage) 

{

try 

    {
        Parent root = FXMLLoader.load(getClass().getResource("../FXML/Login.fxml"));
        Scene scene = new Scene(root);
        primaryStage.setTitle("Login");

        primaryStage.getIcons().add(new Image(Login.class.getResourceAsStream("src/Icons/message.png")));           
        primaryStage.getIcons().add(applicationIcon);
        primaryStage.setScene(scene);
        primaryStage.show();


    } 

catch (IOException e) 

    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

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

However, when running the script to change the application icon, the following error occurs:

Exception in Application constructor java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) 
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source) 
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class application.Login 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$165(LauncherImpl.java:819)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
    ... 1 more 
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found 
    at javafx.scene.image.Image.validateUrl(Image.java:1118) 
    at javafx.scene.image.Image.(Image.java:620) 
    at application.Login.(Login.java:24) 
    ... 13 more 
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1110) 
    ... 15 more
Exception running application application.Login

The files of the program that I am developing, are arranged as follows:

在此输入图像描述

Does anyone know why this error occurs? Thank you.

您需要从路径中删除src。

try to do this

public static final String ICON_IMAGE_LOC = "/icons/logo.png";
public static void setStageIcon(Stage stage) {
    stage.getIcons().add(new javafx.scene.image.Image(ICON_IMAGE_LOC));
    //import javafx.scene.image.Image;
}

and than in the declaration of new stage add it like this

 setStageIcon(stage);

Try using File class

String path = "/src/Icons/message.png";
File fileIcon = new File(path);
Image applicationIcon = new Image(fileIcon.toURI().toString());
primaryStage.getIcons().add(applicationIcon);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM