0

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

3
Contributors
2
Replies
17
Views
8 Years
Discussion Span
Last Post by peter_budo
0

This is the problem

String imgStr = "image/pic1";

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.