我正在拉我的头发。这里有很多像这样的问题,但我无法实现。Java将图像添加到JPanel。为什么图片不显示?
我想添加一个图像到现有的JPanel。问题是让图像在JPanel中可见。该代码运行,但图像是无处..
这里是我的代码:
private void loadImgBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile();
BufferedImage myPicture = null;
try {
myPicture = ImageIO.read(file);
} catch (IOException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
imagePnl2.add(picLabel);
imagePnl2.repaint();
imagePnl2.revalidate();
}
else
{
System.out.println("File access cancelled by user.");
}
}
在this question的问题是缺少revalidate()
。但这在这里没有任何区别。
我错过了什么?
'JLabel picLabel = new JLabel(new ImageIcon(myPicture));'为什么不在启动时添加?无图标或文字的标签无论如何都是看不见的。 – 2014-10-19 21:42:54