[英]image loading using a JFileChooser into a JFrame
I am trying to write a code that displays an image selected using a JFileChooser into another JFrame .I tried the following code below but only got the following errors. 我正在尝试编写一个代码,将使用JFileChooser选择的图像显示到另一个JFrame中。我尝试了下面的代码,但只得到以下错误。
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:228)
at power.<init>(fCGUI.java:53)
at fCGUI.main(fCGUI.java:11)
Here is the code: 这是代码:
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class fCGUI
{
public static void main(String []args)
{
power p=new power();
p.setVisible(true);
}
}
class power extends JFrame
{
JFileChooser chooser;
BufferedImage img;
JButton button,button2;
JFrame comp;
String filename;
File file ;
public power()
{
setSize(450,450);
panel.setLayout(new BorderLayout());
JPanel panel=new JPanel();
getContentPane().add(panel);
button =new JButton("press");
panel.add(button,BorderLayout.NORTH);
chooser = new JFileChooser();
ActionListener action=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==button)
{
chooser.showOpenDialog(null);
file = chooser.getSelectedFile();
try
{
img=ImageIO.read(file);
}
catch(IOException e1) {}
}
if (e.getSource()==button2)
{
comp.setVisible(true);
}
}
};
ImageIcon icon=new ImageIcon(img);
JLabel label=new JLabel(icon);
JPanel secpanel=new JPanel();
comp=new JFrame();
comp.setSize(650,500);
comp.setLayout(new BorderLayout());
comp.setTitle("View Report");
JRootPane compPane=comp.getRootPane();
Container contePane=compPane.getContentPane();
contePane.add(secpanel);
secpanel.add(label,BorderLayout.CENTER);
button2=new JButton("access");
button2.addActionListener(action);
button.addActionListener(action);
panel.add(button2,BorderLayout.SOUTH);
}
}
The value of img
will only have a real value after the user click the button and chooses the file to display. 在用户单击按钮并选择要显示的文件后,
img
的值将仅具有实际值。 Until this time, the value of img
is null
, so when it continues through your method and calls the line ImageIcon icon=new ImageIcon(img);
在此之前,
img
值为null
,因此当它继续通过您的方法并调用行ImageIcon icon=new ImageIcon(img);
, it is trying to create an ImageIcon
object for null
. ,它试图为
null
创建一个ImageIcon
对象。
To correct this, you should only be creating the ImageIcon
when the user has chosen the file. 要更正此问题,您应该只在用户选择文件时创建
ImageIcon
。 Here is a change that should be closer to working correctly. 这是一个应该更接近正常工作的变化。 (see the comments
//ADDED
and //REMOVED
in the code below to see the changes... (请参阅以下代码中的注释
//ADDED
和//REMOVED
以查看更改...
...
class power extends JFrame {
JFileChooser chooser;
BufferedImage img;
JButton button,button2;
JFrame comp;
String filename;
File file ;
JLabel label; // ADDED
public power() {
...
public void actionPerformed(ActionEvent e) {
if (e.getSource()==button) {
chooser.showOpenDialog(null);
file = chooser.getSelectedFile();
try {
img=ImageIO.read(file);
ImageIcon icon=new ImageIcon(img); // ADDED
label.setIcon(icon); // ADDED
Dimension imageSize = new Dimension(icon.getIconWidth(),icon.getIconHeight()); // ADDED
label.setPreferredSize(imageSize); // ADDED
label.revalidate(); // ADDED
label.repaint(); // ADDED
}
catch(IOException e1) {}
}
if (e.getSource()==button2){
comp.setVisible(true);
}
}
};
//ImageIcon icon=new ImageIcon(img); // REMOVED
//JLabel label=new JLabel(icon); // REMOVED
label = new JLabel(); // ADDED
JPanel secpanel=new JPanel();
...
To explain what I've changed... 解释我改变了什么......
label
will now be created as an empty JLabel
when you first start the program. label
现在将创建为空的JLabel
。 It is also stored as a global variable so we can access it later img
is created, as before, and then it is loaded into your label
using setIcon();
img
,然后使用setIcon();
将其加载到您的label
setIcon();
revalidate()
and repaint()
to make sure that the image is drawn after it is set. revalidate()
和repaint()
以确保在设置图像后绘制图像。 The ImageIO.read
is only executed when that specific JButton
is pressed. 只有在按下特定的
JButton
时才会执行ImageIO.read
。 That is because it's inside the ActionListener
. 那是因为它在
ActionListener
。 Because of this, img
is null which causes a NullPointerException
when ImageIcon icon=new ImageIcon(img)
is executed. 因此,
img
为null,当执行ImageIcon icon=new ImageIcon(img)
时会导致NullPointerException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.