JavaFX Application Icon

后端 未结 17 1729
滥情空心
滥情空心 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条回答
  •  猫巷女王i
    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!!

提交回复
热议问题