问题描述
大家好!
我想创建一个JFrame,必须在其上显示计算机上文件中的图像(实际上,我将创建一个Captcha图像).在此图片下,我必须显示一个JTextField(以便在看到上面的图片之后,我可以输入字符串).
我已经这样尝试过:
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:
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();
}
}
我得到的是带有我的图片"bruce-lee2.jpg"的窗口.但是textField尚未出现.但是在最小化窗口之后,我得到了预期的textField.这对我来说听起来很奇怪.
那么有人能告诉我这种情况的原因吗?
渴望得到答复!
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!
推荐答案
setPicture(label, "bruce-lee2.jpg");
到该方法的结尾.
to the end of that method.
这篇关于使用Java中的ImageIcon显示文件中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!