How do I change the default application icon in Java?

后端 未结 10 764
心在旅途
心在旅途 2020-11-27 17:02

I\'m using NetBeans, trying to change the familiar Java coffee cup icon to a png file that I have saved in a resources directory in the jar file. I\'ve found many different

相关标签:
10条回答
  • 2020-11-27 17:15

    You can try this one, it works just fine :

    `   ImageIcon icon = new ImageIcon(".//Ressources//User_50.png");
        this.setIconImage(icon.getImage());`
    
    0 讨论(0)
  • 2020-11-27 17:19

    You can simply go Netbeans, in the design view, go to JFrame property, choose icon image property, Choose Set Form's iconImage property using: "Custom code" and then in the Form.SetIconImage() function put the following code:

    Toolkit.getDefaultToolkit().getImage(name_of_your_JFrame.class.getResource("image.png"))
    

    Do not forget to import:

    import java.awt.Toolkit;
    

    in the source code!

    0 讨论(0)
  • 2020-11-27 17:21

    Or place the image in a location relative to a class and you don't need all that package/path info in the string itself.

    com.xyz.SomeClassInThisPackage.class.getResource( "resources/camera.png" );
    

    That way if you move the class to a different package, you dont have to find all the strings, you just move the class and its resources directory.

    0 讨论(0)
  • 2020-11-27 17:26

    You should define icons of various size, Windows and Linux distros like Ubuntu use different icons in Taskbar and Alt-Tab.

    public static final URL ICON16 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug16.png");
    public static final URL ICON32 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug32.png");
    public static final URL ICON96 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug96.png");
    
    List<Image> images = new ArrayList<>();
    try {
        images.add(ImageIO.read(HelperUi.ICON96));
        images.add(ImageIO.read(HelperUi.ICON32));
        images.add(ImageIO.read(HelperUi.ICON16));
    } catch (IOException e) {
        LOGGER.error(e, e);
    }
    
    // Define a small and large app icon
    this.setIconImages(images);
    
    0 讨论(0)
  • 2020-11-27 17:27
        /** Creates new form Java Program1*/
        public Java Program1() 
    
    
        Image im = null;
        try {
        im = ImageIO.read(getClass().getResource("/image location"));
        } catch (IOException ex) {
        Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex);
        }
        setIconImage(im);
    

    This is what I used in the GUI in netbeans and it worked perfectly

    0 讨论(0)
  • 2020-11-27 17:29

    Try This write after

    initcomponents();
    
    setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("Your image address")));
    
    0 讨论(0)
提交回复
热议问题