JavaFX Application Icon

2019-01-08 04:44发布

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

17条回答
该账号已被封号
2楼-- · 2019-01-08 05:09

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

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

查看更多
女痞
3楼-- · 2019-01-08 05:15
stage.getIcons().add(new Image(ClassLoader.getSystemResourceAsStream("images/icon.png")));

images folder need to be in Resource folder.

查看更多
SAY GOODBYE
4楼-- · 2019-01-08 05:15

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

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-08 05:17

Full program for starters :) This program set Stack Overflow Icon.

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!!

查看更多
Luminary・发光体
6楼-- · 2019-01-08 05:19

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")));
查看更多
再贱就再见
7楼-- · 2019-01-08 05:19

I tried this and it totally works. The code is:

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

icon.png is under the same folder as the source files.

查看更多
登录 后发表回答