尝试更改JavaFX应用程序图标时发生错误

时间:2019-05-17 05:13:44

标签: java javafx icons

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

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);
}

但是,运行脚本更改应用程序图标时,会发生以下错误:

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

我正在开发的程序的文件安排如下:

enter image description here

有人知道为什么会发生此错误吗?谢谢。

3 个答案:

答案 0 :(得分:0)

您需要从路径中删除src。

答案 1 :(得分:0)

尝试执行此操作

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;
}

,然后在新阶段的声明中像这样添加它

 setStageIcon(stage);

答案 2 :(得分:0)

尝试使用File

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