JavaFX Application Icon

后端 未结 17 1551
滥情空心
滥情空心 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:34
    stage.getIcons().add(new Image(ClassLoader.getSystemResourceAsStream("images/icon.png")));
    

    images folder need to be in Resource folder.

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

    Full program for starters :) This program sets icon for StackOverflowIcon.

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.image.Image;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class StackoverflowIcon extends Application {
    
        @Override
        public void start(Stage stage) {
            StackPane root = new StackPane();
            // set icon
            stage.getIcons().add(new Image("/path/to/stackoverflow.jpg"));
            stage.setTitle("Wow!! Stackoverflow Icon");
            stage.setScene(new Scene(root, 300, 250));
            stage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    Output Screnshot

    JavaFX Screenshot

    Updated for JavaFX 8

    No need to change the code. It still works fine. Tested and verified in Java 1.8(1.8.0_45). Path can be set to local or remote both are supported.

    stage.getIcons().add(new Image("/path/to/javaicon.png"));
    

    OR

    stage.getIcons().add(new Image("https://example.com/javaicon.png"));
    

    enter image description here

    Hope it helps. Thanks!!

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

    I used this in my application

    Image icon = new Image(getClass().getResourceAsStream("icon.png"));
    window.getIcons().add(icon);
    

    Here window is the stage.

    0 讨论(0)
  • 2020-11-28 02:39
    stage.getIcons().add(new Image("/images/logo_only.png"));
    

    It is good habit to make images folder in your src folder and get images from it.

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

    If you got Invalid URL or resource not found put your icon.png in the "bin" folder in your workspace.

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

    you can add it in fxml. Stage level

    <icons>
        <Image url="@../../../my_icon.png"/>
    </icons>
    
    0 讨论(0)
提交回复
热议问题