I'm trying to get a GUI application to display an image. So far I've got this bit of code:

import javax.swing.*;
import java.awt.*;

public class DemoImage extends JFrame {
	public void showImage() {
		
				
		// creates the actual frame with title 'My GUI' and dimensions
		JFrame frame = new JFrame("My GUI");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(200, 50);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		
		// Inserts the image icon
		String imgStr = "image/pic1";
			
			
		ImageIcon image = new ImageIcon(imgStr);
		JLabel label1 = new JLabel(" ", image, JLabel.CENTER);
		frame.getContentPane().add(label1);
		
		frame.validate();
		frame.setVisible(true);
		
		
		
	}
	public static void main(String[] args) {
		DemoImage show1 = new DemoImage();
		show1.showImage();
	}

}

When i run it all i get is a window with a title "MY GUI". What else should i add so the image is actually displayed?

Thanks in advance

Recommended Answers

All 2 Replies

I don't know for sure by try the full path of the image with the extension

This is the problem

String imgStr = "image/pic1";

, you did not provide file extension like pic1.jpeg, pic1.png, pic1.gif

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.