Set Icon on Stage in JavaFX

2019-04-23 14:45发布

I wanted to know how should I set icons on javaFX stage. I have found this method, but it did not work properly.

  stage.getIcons().add(new Image(iconImagePath));

stage is an instance of javafx.stage.Stage, and I have imported javafx.scene.image.Image. This is the exception which we receive:

Invalid URL: Invalid URL or resource not found

Also, there is nothing wrong with the iconImagePath, its value is "G:/test.jpg" and there is a jpg file in the G drive named test. In addition, when we use ImageIO to read the same URL we can do it easily.

11条回答
Deceive 欺骗
2楼-- · 2019-04-23 15:18
stage.getIcons().add(new Image(getClass().getResourceAsStream("bal.png")));

This example works. I placed an icon in the same folder/package as the source .java file.

Directory structure

查看更多
萌系小妹纸
3楼-- · 2019-04-23 15:20
public class Main extends Application
{
    private static final Logger LOGGER = Logger.getLogger(Main.class);

    @Override
    public void start(Stage primaryStage)
        {
            try
                {
                    // BorderPane root = new BorderPane();
                    BorderPane root = (BorderPane) FXMLLoader
                            .load(getClass().getResource("/org/geeksynergy/view/layout/FrontPageBorder.fxml"));
                    root.setAccessibleText("good");

                    Scene scene = new Scene(root, 400, 400);
                    scene.getStylesheets().add(getClass()
                            .getResource("/org/geeksynergy/view/cssstyle/application.css").toExternalForm());
                    primaryStage.setScene(scene);
                    primaryStage.setTitle("AiRJuke");
                    primaryStage.getIcons().add(new Image("/org/geeksymergy/resource/images/download.png"));
                    primaryStage.show();
                    AnchorPane personOverview = (AnchorPane) FXMLLoader
                            .load(getClass().getResource("/org/geeksynergy/view/layout/Ui.fxml"));

                    root.setCenter(personOverview);

                    // added this line to save the playlist , when we close
                    // application window
                    Platform.setImplicitExit(false);

                    primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>()
                        {
                            public void handle(WindowEvent event)
                                {
                                    M3UPlayList.defaultSavePlaylist();
                                    Platform.setImplicitExit(true);
                                    primaryStage.hide();
                                }
                        });

                } catch (Exception e)
                {

                    LOGGER.error("Exception while loding application", e);
                }
        }

    public static void main(String[] args)
        {
            launch(args);
        }
}
查看更多
ゆ 、 Hurt°
4楼-- · 2019-04-23 15:21

The constructors of javafx.scene.image.Image expect a URI, not a (full) path. This URI can either be relative (e.g. /images/flower.png) or absolute (e.g. file:flower.png).

Strings like G:/test.jpg are no valid URLs and hence illegal.

Try file:g:/test.jpg instead.

Usually, the icons should be bundled with your application, so simply put the image file into your classpath (e.g. if you're using eclipse, put it into your 'src' directory) and use it like that:

stage.getIcons().add(new Image("/logo.jpg"));
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-04-23 15:22

don't forget that your icon must be in 32x32 or 16x16 resolution, if not, it doesn't work.

查看更多
不美不萌又怎样
6楼-- · 2019-04-23 15:22

I use netbeans 8.2, if I use : stage.getIcons().addAll(new Image(getClass().getResourceAsStream("home-icon32.png")));

I have to put the image in src directory. Don't know why, but works only this way. I've tried putting it in build/classes, but negative.

查看更多
闹够了就滚
7楼-- · 2019-04-23 15:24

The solution is:

File f = new File("image.png");
Image ix = new Image(f.toURI().toString());
stage.getIcons().add(ix);
查看更多
登录 后发表回答