如何使用JFilechooser将图片添加到JButton中

时间:2014-01-18 14:30:11

标签: java swing jbutton jfilechooser

JFileChooser无法工作我在网上收到了错误:

btnNewButton.setIcon(new ImageIcon(ImageIO.read(file)));

请帮帮我,我是初学者。它给了我这个错误:

  

线程中的异常" AWT-EventQueue-0" java.lang.IllegalArgumentException:input == null!       在javax.imageio.ImageIO.read(未知来源)       在RecordManagementSystem.addRecord $ 1.actionPerformed(addRecord.java:185)       在javax.swing.AbstractButton.fireActionPerformed(未知来源)       at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)       在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)       在javax.swing.DefaultButtonModel.setPressed(未知来源)       在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源)       at java.awt.Component.processMouseEvent(Unknown Source)       在javax.swing.JComponent.processMouseEvent(未知来源)       at java.awt.Component.processEvent(Unknown Source)       at java.awt.Container.processEvent(Unknown Source)       at java.awt.Component.dispatchEventImpl(Unknown Source)       at java.awt.Container.dispatchEventImpl(Unknown Source)       at java.awt.Component.dispatchEvent(Unknown Source)       at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)       at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)       at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)       at java.awt.Container.dispatchEventImpl(Unknown Source)       at java.awt.Window.dispatchEventImpl(Unknown Source)       at java.awt.Component.dispatchEvent(Unknown Source)       at java.awt.EventQueue.dispatchEventImpl(Unknown Source)       在java.awt.EventQueue.access $ 000(未知来源)       在java.awt.EventQueue $ 1.run(未知来源)       在java.awt.EventQueue $ 1.run(未知来源)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.AccessControlContext $ 1.doIntersectionPrivilege(Unknown Source)       at java.security.AccessControlContext $ 1.doIntersectionPrivilege(Unknown Source)       在java.awt.EventQueue $ 2.run(未知来源)       在java.awt.EventQueue $ 2.run(未知来源)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.AccessControlContext $ 1.doIntersectionPrivilege(Unknown Source)       at java.awt.EventQueue.dispatchEvent(Unknown Source)       at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)       在java.awt.EventDispatchThread.run(未知来源)

代码:

btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser fc = new JFileChooser();
            File file = fc.getSelectedFile();
            int result = fc.showOpenDialog(null);

            if (result == JFileChooser.APPROVE_OPTION) {              
                try {
                    btnNewButton.setIcon(new ImageIcon(ImageIO.read(file)));
                } catch (IOException e) {
                    JOptionPane.showMessageDialog(null, e);
                }
            }
        }
    });

1 个答案:

答案 0 :(得分:3)

您在选择之前从JFileChooser 获取该文件,因此fc.getSelectedFile()将返回null

解决方案:在显示JFileChooser后,在if块中获取文件。

if (result == JFileChooser.APPROVE_OPTION) {              
    try {
        File file = fc.getSelectedFile();  // **** line added ****
        btnNewButton.setIcon(new ImageIcon(ImageIO.read(file)));
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null, e);
    }
}

另外,我喜欢避免过度压缩我的代码,以便在发生任何错误时轻松查找错误。所以我会这样做:

if (result == JFileChooser.APPROVE_OPTION) {              
    try {
        File file = fc.getSelectedFile();
        Image image = ImageIO.read(file);
        Icon icon = new ImageIcon(image);
        btnNewButton.setIcon(icon);
    } catch (IOException e) {
        // log error
    }
}