开发者

Set Icon Image in Jar file

开发者 https://www.devze.com 2022-12-24 02:34 出处:网络
The following code works fine when running on NetBeans. this.getFrame().setIconImage(Toolkit.getDefaultToolkit().getImage(\"PlagiaLyzerIcon.png\"));

The following code works fine when running on NetBeans.

this.getFrame().setIconImage(Toolkit.getDefaultToolkit().getImage("PlagiaLyzerIcon.png"));

However, once it was built into Jar file, the icon was gone.

Anyone has idea what's the problem? I realized I've to put the icon image on the root directory, however, after compiling into JAR, the icon gone.

Thanks for any help ...

Hi everyone, the problem was solved with the following code,

 this.getFrame().setIconImage(Toolkit.g开发者_运维百科etDefaultToolkit().getImage(getClass().getClassLoader().getResource("plagialyzer/resources/PlagiaLyzerIcon.png")));

It works once compiled into jar file.


Use

this.getFrame().setIconImage(
new imageIcon(getClass().getClassLoader().getResource("PlagiaLyzerIcon.png"))
);

instead.

Note:

this line only works if the images are in the root of the jar file. If not, you have to specify the folder on the string:

getResource("yourfolder/PlagiaLyzerIcon.png")


That is because the Netbeans IDE has a different classpath, than when running the jar-file stand-alone (without Ant).

Assume your Netbeans project is at location /project/:

The classpath is: /project/build/classes/ and the project root /project/. If your icons are stored in: /project/myicons/, then they are part of the classpath, since /project/ is too. But when you build your project, only files in /project/build/classes/ will eventually end up in the jar-file, these files are "build" from /projcet/src/.

Solution:

Move your icons into a source-package: /project/src/myicons/

Or, add the /project/myicons/ folder to your sources (right-click your project -> Properties -> Sources -> add your folder there)


Thanks a lot...

Here the code that works for me!!!!

    BufferedImage image = null;

    try {
        image = ImageIO.read(getClass().getClassLoader().getResource("images/frame.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }


    super.setIconImage(image);


Did you specify the build path to your icons in the options of Netbean before exporting the JAR? On Eclipse it's done by adding a source folder inside the Java Build Path as shown in this screenshot. Should be the same way on Netbeans?


Using NetBeans like myself, just put the logo (in my case mylogo.png) inside src/ directory in your project folder. Make sure size is 32 x 32 pixel to ensure best quality for icon. I couldn't manage to set .ico images as my JFrame icon but .png worked well. By extending JFrame, I directly set my jar icon with:

Image getIcon()
{ return Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("mylogo.png")); }

setIconImage(getIcon());

After clean and build jar, the jar file icon still isn't changed (still the jar default icon) but when you execute the jar file the icon in title bar and taskbar will be your custom icon. You can change the jar file icon by creating shortcuts and change icon for that shortcut. Best resolution for shortcut icon is 512 x 512 pixel .ico


I use this code, but it only changes the little icon that you have top left on your windows and the icon that you have at the button on the bar where you can see which app is open.

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

constuctor of my class extends JFrame{
   BufferedImage image = null;
   try {
      image = ImageIO.read(new File("path/logo.png"));
   } catch (IOException e) {
      e.printStackTrace();
   }
   super.setIconImage(image);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号