简体   繁体   中英

Java issue loading image

for some reason i cannot load certain images with my java program. just above this snippet of code i have another image referral that works fine.

for(int x = 1; x<=7; x++){
        if(additionals[x] != 0){
            rightPanel.add(new JLabel(new ImageIcon("IMAGES/image"+x+".GIF")));
            count++;
        }
    }

images are saved in a folder named IMAGES and are called image1.gif, image2.gif etc if you need the rest of my code just ask

  1. is it windows? GIF != gif otherwise
  2. does it work if you remove condition (additionals[x] != 0) ?
  3. is rightPanel big enough for all images?

Are you sure that this:

    if (additionals[0] != 0){
        rightPanel.add(new JLabel(new ImageIcon("IMAGES/ram"+additionals[0]+"gb.gif")));
        count++;
    }
    for(int x = 1; x<=7; x++){
        if (additionals[x] != 0){
            rightPanel.add(new JLabel(new ImageIcon("IMAGES/image"+x+".gif")));
            count++;
        }
    }

shouldn't really be this?

    if (additionals[0] != 0){
        rightPanel.add(new JLabel(new ImageIcon("IMAGES/ram"+additionals[0]+"gb.gif")));
        count++;
    }
    for(int x = 1; x<=7; x++){
        if (additionals[x] != 0){
            rightPanel.add(new JLabel(new ImageIcon("IMAGES/image"+ additionals[x]+".gif")));
            count++;
        }
    }

?

It would make your code appear more symmetric. Otherwise, do printlns before the offending line with the String that you want to use to make the ImageIcon to be sure that it's correct.

For example:

    for(int x = 1; x<=7; x++){
        if (additionals[x] != 0){
            String imagePath = "IMAGES/image"+x+".gif";
            System.out.println("imagePath = " + imagePath);
            rightPanel.add(new JLabel(new ImageIcon(imagePath)));
            count++;
        }
    }

And then compare the Strings that are output with the file names and paths. Even better is to create a new File and output its full path before trying to use it to create a new ImageIcon.

caveat: code has not been tested.

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