I'm not sure where the problem is. Is it in the path? The image doesn't show although there are no errors at all in the code syntax. Should I provide the whole path or just place the image in the directory and call its name? Thank you.
public class NetworkingGame {
private JFrame jfrm;
NetworkingGame(){
jfrm = new JFrame("Angry Painters");
jfrm.setSize(800, 480);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setVisible(true);
}// end of constructor
public void ImageLoading(){
ImageIcon i = new ImageIcon("C:/Users/TOSHIBA/Documents/NetBeansProjects/NetworkingGame/build/classes/angry-painters.jpeg");
JLabel jl = new JLabel(i);
jfrm.add(jl);
}
public static void main(String[] args) throws Exception{
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
NetworkingGame ng = new NetworkingGame();
ng.ImageLoading();
} // end of run method
}); // end of Runnable
}//end of main method
}//end of class NetworkingGame
Do not use the file path as icon location as this will only work on your computer. You really cannot expect all machines in the world to have C:/Users/TOSHIBA ...angry-painters.jpeg in the right place!
Copy the file next to the source code (.java) class you use and then call
new ImageIcon(getClass().getResource("angry-painters.jpeg"));
The builder should copy the image resource to the class folder itself.
Try to use path something like this :
jLabel1.setIcon(new ImageIcon("c:\\Users\\xyz\\Documents\\NetBeansProjects\\game\\src\\images.jpg"));
Update
If dis also does not work, then
jLabel.setIcon(new ImageIcon(getClass().getResource("/image.jpg")));
jLabel.setText("jLabel");
should.The image.jpg should be inside your project folder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With