java - Image cannot show on Jbutton -


I do not know where I've gone wrong, I have tried all possible ways to display an image on the pocket, but Can not understand why the getResource class brings a null pointer. I have also checked the path I have used repeatedly, but the path I provided is correct.

Please help

  Public category media has expanded by JPanel {// Our Media Player Component declares personal JPanel video_pnl; Private pocket play_btn; Private JLabel loc_lbl; Private Incharge; ArrayList & LT; String & gt; file location; Public media (ArrayList & lt; string & gt; file_location) {this.file_location = file_location; Growth = 0; While (increment & lt; file_location.size ()) {video_pnl = new JPanel (); Video_pnl.setLayout (New BoxLayout (video_pnl, BoxLayout.Y_AXIS)); Loc_lbl = new JLab (); Loc_lbl.setText (file_location.get) (increments)); Play_btn = New Payton ("Play"); Image Icom IMG = New Imagen (this.getClass (). GetResource ("a.png")); Play_btn.setIcon (img); Video_pnl.add (loc_lbl); Video_pnl.add (play_btn); (Video_pnl); ++; }} Public static zero main (string [] args) {// Announce local characters and start ArrayList & lt; String & gt; File_location = New Arreelist & lt; & Gt; (); File_location.add ('A'); File_location.add ("b"); // vlcplayer creates examples of objects, pass media paces and press "run" media media player = new media (file_space); JFrame ourframe = new JFrame (); Ourframe.setContentPane (mediaplayer); Ourframe.setLayout (new grid layout (5, 1)); Ourframe.setSize (300, 560); Ourframe.setVisible (true); Ourframe.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); }}  

I tested your code and it works fine Your image is incorrect In the situation most likely.

If you are going to use just then click on the image file name as the path "a.png" , then the image Should be in the same package that is calling your class. For example

  ProjectRoot src some.package Media.java a.png  

On the build, a copy of the image class path will be received, which will be Same package as Media.class


Comments