大家好,我的目的是想做一个可以在JPanel里通过点击按钮激活文件对话框来打开图片文件的一个小程序,但是有个异常,请麻烦各位看下:import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;class BackgroundPanel extends JPanel implements ActionListener {
    ImageIcon icon;
    JButton b1;
    JFileChooser fileChooser = null;
    String str = null;
    Graphics g = getGraphics();
    
    public BackgroundPanel() {
     b1 = new JButton("打开文件");
        b1.addActionListener(this);
        this.add(b1);        fileChooser = new JFileChooser("E:\\");
        
    }
    
    public void actionPerformed(ActionEvent e)
    {
        File file = null;
        int result;        if (e.getActionCommand().equals("打开文件"))
        {
            fileChooser.setApproveButtonText("打开吗");
            fileChooser.setDialogTitle("确定");
            result = fileChooser.showOpenDialog(this);            if (result == JFileChooser.APPROVE_OPTION)
            {
                file = fileChooser.getSelectedFile();
                str = file.getName();
                icon = new ImageIcon(BackgroundPanel.class.getResource(str));
        // this.setPreferredSize(new Dimension(467, 362));
         PaintDemo(g);
            }
     
        }
    }
    public void PaintDemo(Graphics g) {
        g.drawImage(icon.getImage(), 0, 0, this);
    }
}class my extends JFrame {

BackgroundPanel bp;

public my() {
bp = new BackgroundPanel();
this.getContentPane().add(bp);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
}
public static void main(String[] args) {
     new my();
    }
}

解决方案 »

  1.   

    //改了下你的类BackgroundPanel 和PaintDemo 方法
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JPanel;/**
     * @author dido leo
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class BackgroundPanel extends JPanel implements ActionListener {
    ImageIcon icon;
        JButton b1;
        JFileChooser fileChooser = null;
        String str = null;
        Graphics g = getGraphics();
        
        public BackgroundPanel() {
         b1 = new JButton("打开文件");
            b1.addActionListener(this);
            this.add(b1);        fileChooser = new JFileChooser("E:\\");
            
        }
        
        public void actionPerformed(ActionEvent e)
        {
            File file = null;
            int result;        if (e.getActionCommand().equals("打开文件"))
            {
                fileChooser.setApproveButtonText("打开吗");
                fileChooser.setDialogTitle("确定");
                result = fileChooser.showOpenDialog(this);            if (result == JFileChooser.APPROVE_OPTION)
                {
                    file = fileChooser.getSelectedFile();
                   
                    str= file.getAbsolutePath();
                   icon = new ImageIcon(str);
                    
            // this.setPreferredSize(new Dimension(467, 362));
             PaintDemo();
                }
         
            }
        }
        
        public void PaintDemo() {
        
            repaint();    
        }    public void paintComponent(Graphics g){
         //
         this.g=g;
         if(icon!=null)
         g.drawImage(icon.getImage(), 0, 0, this);
        
        }}
      

  2.   

    我用的是 file.getAbsolutePath();
      

  3.   

    使用JLabel组件不是更好吗.
    button.onclick(){
       jPane.add(new JLabel(new ImageIcon("filepath")),BorderLayout.CENTER);
    }