JavaFX Application Icon

后端 未结 17 1549
滥情空心
滥情空心 2020-11-28 02:25

Is it possible to change the application icon using JavaFX, or does it have to be done using Swing?

相关标签:
17条回答
  • 2020-11-28 02:28

    You can easily put icon to your application using this code line

    stage.getIcons().add(new Image("image path") );

    0 讨论(0)
  • 2020-11-28 02:29

    If you run the jar file, the code specified by Michael Berry will change the icon in the title bar and in the taskbar. Shortcut icon cannot be changed.

    If you run a native program compiled with com.zenjava, You must add a link to the program icon:

    <plugin>
        <groupId>com.zenjava</groupId>
        <artifactId>javafx-maven-plugin</artifactId>
        <version>8.8.3</version>
        <configuration>
        ...
            <bundleArguments>
                <icon>${project.basedir}/src/main/resources/images/filename.ico</icon>
            </bundleArguments>
        </configuration>
    </plugin>
    

    This will add an icon to the shortcut and taskbar.

    0 讨论(0)
  • 2020-11-28 02:30
    stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("/icon.png" )));
    

    You can add more than one icon with different sizes using this method.The images should be different sizes of the same image and the best size will be chosen. eg. 16x16, 32,32

    0 讨论(0)
  • 2020-11-28 02:32

    Assuming your stage is "stage" and the file is on the filesystem:

    stage.getIcons().add(new Image("file:icon.png"));
    

    As per the comment below, if it's wrapped in a containing jar you'll need to use the following approach instead:

    stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("icon.png")));
    
    0 讨论(0)
  • 2020-11-28 02:32

    What do you think about creating new package i.e image.icons in your src directory and moving there you .png images? Than you just need to write:

    Image image = new Image("/image/icons/nameOfImage.png");
    primaryStage.getIcons().add(image);
    

    This solution works for me perfectly, but still I'm not sure if it's correct (beginner here).

    0 讨论(0)
  • 2020-11-28 02:34

    If you have have a images folder and the icon is saved in that use this

    stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("/images/comparison.png")));
    

    and if you are directly using it from your package which is not a good practice use this

    stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("comparison.png")));
    

    and if you have a folder structure and you have your icon inside that use

    stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("../images/comparison.png")));
    
    0 讨论(0)
提交回复
热议问题