求教,为什么我这窗体的图片显示不出来
我想把一个路径为E:\\未命名2.JPG的图片显示到窗体上,但是为啥显示不出来呢?代码如下:package ft;
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import
import
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MyTest extends JFrame implements ActionListener{
JButton jbtn = null;
JTextField jtf = null;
JPanel bp1 = new JPanel();
JPanel bp2 = new JPanel();
public MyTest(){
this.setLayout(new FlowLayout());
jbtn = new JButton("打开..");
jbtn.addActionListener(this);
jtf = new JTextField(30);
ImageP p = new ImageP();
bp2.add(p);
bp1.add(jtf);
bp1.add(jbtn);
this.add(bp1,BorderLayout.SOUTH);
this.add(bp2,BorderLayout.NORTH);
this.setSize(400,400);
this.setVisible(true);
this.pack();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(jbtn)){
int response = 1;
do{
FileDialog fd = new FileDialog(this);
fd.setVisible(true);
String a = fd.getDirectory()+fd.getFile();
System.out.println(a);
this.setVisible(false);
Object[] options = {"是,我要继续生成新XML","否,我想退出程序"};
response=JOptionPane.showOptionDialog(this, "XML已生成,是否继续生成新XML?", "亿阳报表自动化系统",JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if(response == 1)
{
System.exit(0);
}
}while(response == 0);
}
}
public static void main(String[] args){
new MyTest();
}
}
class ImageP extends JPanel{
JPanel bp2 = new JPanel();
private Image image;
public ImageP(){
try{
image = ImageIO.read(new File("E:\\未命名2.JPG"));
}catch(IOException e){
e.printStackTrace();
}
if(image == null){
System.out.println("222");
}else{
System.out.println("333");
}
}
public void ImageP2(Graphics g){
super.printComponents(g);
g.drawImage(image,0,0,null);
}
}