What is the correct path to display an ImageIcon png file for Windows 7?

前端 未结 6 1637
南笙
南笙 2020-12-04 02:20

I wanted to test having a program with a simple png image on it. I wrote a short program that does this, but I can\'t seem to get the path right. I have checked, checked aga

相关标签:
6条回答
  • 2020-12-04 02:48

    Using this fixed it for me:

    JButton btnBanana = new JButton("New button");
    btnBanana.setIcon(new ImageIcon("D:\\Android\\Company\\images\\bananas-icon.png"));
    
    0 讨论(0)
  • 2020-12-04 02:51

    The getResource(String) method will only find resources that are on the run-time class-path of the application. Since this image seems like an application resource (i.e. supplied by you as part of the application) it should be put on the run-time class-path.

    E.G. Most IDEs have a place you can put resources within the project structure, that will automatically be included at run-time. Move (or copy) the image to that path.

    Then it becomes a matter of providing the correct String. Let us imagine your project is set up something like this:

    • bin
    • src
      1. com
        • our
          1. Application.java
      2. resources
        • green.png

    So Application.java is in package com.our;, while the image is in the path resources/green.png.

    If accessing the image from the Application, the correct path would be (drum roll please..)

    "/resources/green.png"

    Notes

    1. The leading / is important. It tells the JRE we want to look for the image from the 'root of the class-path', as opposed to using a path relative to the package of the class itself.
    2. Correct case is also vital. A string of "/resources/green.png" will not locate an image named "/resources/Green.png" or "/resources/green.PNG".

    Eclipse paths

    1. Right click on the src directory, select Properties at the bottom of the menu.
      Eclipse properties
    2. Navigate (using the normal way you'd use without Eclipse) to the directory of the Location. Eclipse properties showing project Location
    3. Then go to the parent directory.
    4. You should see a bin directory that contains classes and (hopefully) the image.
    0 讨论(0)
  • 2020-12-04 02:55

    You need to do C:\\Test\\test.png and not C:/Test/test.png

    0 讨论(0)
  • 2020-12-04 02:56

    To get the path of a image to a text filed, this code will help you

    txtPath.setText(lblImage.getIcon().toString());
    

    //txtPath is the text filed use todiplay the path //lblImage is the label which shows the image

    0 讨论(0)
  • 2020-12-04 02:59

    Firstly, you've supplied a relative path, so the system is looking for the image relative to the location you executed the program.

    Secondly, the path should have a drive spec or at least a leading /. Depending on your setup, something like 'C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png' should work (you may need to change the drive spec to meet your system)

    Thirdly, make sure that the file exists in the specified location, System.out.println(new File("C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png").exists()) should return true, other wise the file is in the wrong location.

    A relative path basically means a path location relative to the programs execution. So, if you were running the program from C:/Program Files/MyAwesomeApplication for example, a relative path of Users/Evan/javaItems/Sprites_and_Other_Art/green.png would become an absolute path of C:/Program Files/MyAwesomeApplication/Users/Evan/javaItems/Sprites_and_Other_Art/green.png. This describes the path from the root location to the file/folder in question.

    You can test this by using System.out.println(new File("C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png").getAbsolutePath(‌​)) which will give you the full path.

    0 讨论(0)
  • 2020-12-04 03:06

    use double slash instead of one , i had this problem and i fixed it . ill show you an example :

    public Driver (){

        ImageIcon us = new ImageIcon("C:\saeed.gif"); // OS cant find it 
        ImageIcon uk = new ImageIcon("C:\\saeed0.gif"); // OS can 
    
    
      JButton button = new JButton ("Click here "  , us ) ;
    
      button.setRolloverIcon(uk);
    
      add(button);
    }
    
    0 讨论(0)
提交回复
热议问题