[英]Jframe becomes empty on adding image
I need to display an image from my pc file system. 我需要从我的电脑文件系统中显示一个图像。 The image is stored at -
图像存储在 -
C:\\Users\\Akshay\\Desktop\\allfiles C:\\ Users \\用户阿克沙伊\\桌面\\ allfiles
The relevant code is (scroll to right) : 相关代码是(向右滚动):
JFrame frame = new JFrame();
frame.setExtendedState( frame.getExtendedState()|JFrame.MAXIMIZED_BOTH );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLayout(new FlowLayout());
JLabel title = new JLabel("Image : ");
ImageIcon image = new ImageIcon("C:\\Users\\Akshay\\Desktop\\allfiles\\sampleposter.jpg"); //line #1
JLabel l = new JLabel("");
l.setIcon(image); // line #2
frame.add(title);
frame.add(l);
Following is the output. 以下是输出。 I am getting an empty frame instead of two jlabels.
我得到一个空框架而不是两个jlabels。
On commenting line #2 in the code ie - 在代码中注释第2行,即 -
l.setIcon(image);
I get the same output instead of two simple JLabels
我得到相同的输出而不是两个简单的
JLabels
On commenting both line #1 and line #2 , I am getting the two labels (one empty) as shown below. 在评论第1行和第2行时,我得到两个标签(一个空),如下所示。
How do I display the image properly with the other label? 如何使用其他标签正确显示图像? What could be the problem here?
这可能是什么问题?
you added JLabel title = new JLabel("Image : "); 你添加了JLabel title = new JLabel(“Image:”); to the already visible Swing GUI, after setVisible(true);
在setVisible(true)之后到已经可见的Swing GUI; was called,
被称为,
move code lines creating JLabel with ImageIcon before frame.setVisible(true); 移动代码行在frame.setVisible(true)之前使用ImageIcon创建JLabel;
move code line frame.setLayout(new FlowLayout()); 移动代码行frame.setLayout(new FlowLayout()); before frame.setVisible(true);
在frame.setVisible(true)之前;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.