Reading an image in Netbeans

痴心易碎 提交于 2019-11-27 22:28:33

Try

ImageIO.read(Mine.class.getResource("../minesscce.resources/Manling.png")); 

Here's an example:

  • Hierarchy

  • Result

And here's the code...

public final class ImageResourceDemo {     private static BufferedImage bi;      public static void main(String[] args){         try {             loadImage();              SwingUtilities.invokeLater(new Runnable(){                 @Override                 public void run() {                     createAndShowGUI();                              }             });         } catch (IOException e) {             e.printStackTrace();         }     }      private static void loadImage() throws IOException{         bi = ImageIO.read(                 ImageResourceDemo.class.getResource("../resource/avatar6.jpeg"));     }      private static void createAndShowGUI(){         final JFrame frame = new JFrame();         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         frame.getContentPane().setBackground(Color.WHITE);         frame.add(new JLabel(new ImageIcon(bi)));         frame.pack();         frame.setLocationRelativeTo(null);         frame.setVisible(true);     } } 

If I am not wrong, root directory of your application is the project directory or the source directory. (Not sure exactly which one is)

If it is project directory then resources/Manling.png is MineSSCCE/resources/Manling.png. Nothing is there!

If it is the source directory, resources/Manling.png is MineSSCCE/Source/resources/Manling.png. Nothing is there either!

The actual location is MineSSCCE/Source/minesscce/resources/Manling.png That is why it was not working.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!