Java:如何在jar(或exe)中放置一个图标?它的路径是什么?

时间:2016-02-27 17:30:22

标签: java icons

我想要做的是在程序运行时更改程序的图标。

我发现的代码是:

      try {
          String imagePath = "res/Image.png";
          InputStream imgStream = this.getClass().getResourceAsStream(imagePath );
          BufferedImage myImg;
          myImg = ImageIO.read(imgStream);
          this.setIconImage(myImg);
      } catch (IOException e) {
          e.printStackTrace();
      }

我的问题是:我在哪里放置图片以及如何获取其路径?最终的结果是有一个.exe(或者最坏的情况是.jar),我只需要转移,所以我想在exe中有图标。

1 个答案:

答案 0 :(得分:0)

import {Component} from 'angular2/core';
import {CORE_DIRECTIVES, NgClass, FORM_DIRECTIVES, Control, ControlGroup, FormBuilder, Validators} from 'angular2/common';

class DemoInfo{
  name:string;
  password: string;
  radio: any;
  select: any;
}
@Component({
    selector: 'my-app',
    templateUrl: 'mytemplate.html',
    directives: [CORE_DIRECTIVES, FORM_DIRECTIVES] 
})
export class AppComponent { 
  CreateGroup: FormBuilder;
  demoInfo: DemoInfo;
  constructor(fb: FormBuilder){
    this.demoInfo= new DemoInfo(); 

    this.CreateGroup = fb.group({
            'name': new Control(this.demoInfo.name, Validators.required),
            'password': new Control(this.demoInfo.password, Validators.required),
            'select': new Control(this.demoInfo.select, Validators.required)
        })
  }
  addNewGroup(demoInfo:demoInfo) {
    console.log(demoInfo, 'whole object');
    this.demoInfo= new DemoInfo();
  }
}

如果使用/ character:

启动它,也可以在jar中指定绝对文件夹
Class.getResource(String name) expects a resource name relative to the location the class is loaded from. So if your resource is called image.jpg, then image.jpg must be in the same folder where the class is whose getResource() method you're calling.

在这种情况下,image.jpg必须位于jar内部的/ resources / images文件夹中或源文件夹中(由IDE将其复制到bin文件夹中)。