Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all of you!

I wanna create a JFrame, on which I must display an image from a file on my computer (Actually I'm going to create a Captcha image). Under this picture I must show a JTextField (so that after, let's say, seeing the picture above, I can enter the string).

I've tried like this:
Java
package drawingUsingImageIcon;

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

public class ImageIconDemo extends JFrame{
	private ImageIcon icon;
	public ImageIconDemo()
	{
		super("Image Icon Demo");
		setVisible(true);
		setSize(330, 260);
		JLabel	label = new JLabel();
				
		setLayout(new FlowLayout(FlowLayout.CENTER));
		
		label.setSize(270, 200);
		add(label);
		setPicture(label, "bruce-lee2.jpg");
		
		JTextField textField = new JTextField(20);
		textField.setVisible(true);
		add(textField);
	}
	private void setPicture(JLabel label, String fileName)
	{
		icon = new ImageIcon(fileName);
		label.setIcon(icon);
	}
	public static void main(String[] args)
	{	
		ImageIconDemo frame = new ImageIconDemo();
	}
}

What I've got is that a window with my picture "bruce-lee2.jpg". But the textField haven't appeared. But after I've minimized the window, I've got the textField as I expected. That sounds very strange to me.

So could any body tell me what is the reason in this case?

Longing for the replies!

Thank you in advanced!
Posted

1 solution

move the line

Java
setPicture(label, "bruce-lee2.jpg");


to the end of that method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900