Java - ImageIcon won't show image

后端 未结 2 449
情歌与酒
情歌与酒 2021-01-26 09:30
searchIcon = new ImageIcon( \"searchIcon.png\" );

searchIconLabel = new JLabel();
searchIconLabel.setIcon(searchIcon);

....

searchHorizontalPanel.add( searchIconLabel         


        
2条回答
  •  没有蜡笔的小新
    2021-01-26 10:08

    Your image file needs to be in the same directory as you are running the application from.

    If its actually in the same directory as the class making the call then you can use:

    searchIcon = new ImageIcon( getClass().getResource("searchIcon.png") );
    

    If it is in some arbitrary location then you will need to specify the full path.

提交回复
热议问题