简体   繁体   中英

How do i put a icon to jLabel

By default it has an image with the path /img/profileicon/29.png but then from the main I want to change it, but for some reason instead of changing it disappears.

The code:

int iconId = summoner.getProfileIconId();
        ImageIcon img = new javax.swing.ImageIcon("/img/profileicon/"+iconId+".png");
        profileIconImg.setIcon(img);

Finally I was able to use this code, making use of class.getResource

URL iconUrl = EuwGG.class.getResource("/img/profileicon/"+profileIconId+".png");
    Image profileImage = ImageIO.read(iconUrl);
    ImageIcon profileIcon = new ImageIcon(profileImage);
    Image i = profileIcon.getImage().getScaledInstance(125, 125, Image.SCALE_SMOOTH);
    profileIcon = new ImageIcon(i);
    profileIconImg.setIcon(profileIcon);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM