2014-01-18 31 views
0

添加图片到的JButton JFileChooser将无法​​工作,我在网上遇到一个错误:如何使用的JFileChooser

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

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

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(Unknown Source) at RecordManagementSystem.addRecord$1.actionPerformed(addRecord.java:185) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) 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) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) 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) at java.awt.EventDispatchThread.run(Unknown Source)

代码:

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); 
       } 
      } 
     } 
    }); 
+3

我可以建议你修改你的错误捕获策略,而不是仅仅抛出错误为'JOptionPane',考虑的方式,您可以查看它记录错误。在我的应用程序中,我想知道所引发错误的110%。 – ryvantage

+0

谢谢你的推荐=) – dens14345

回答

3

你从之前它已被选中,所以fc.getSelectedFile()将返回null JFileChooser中获取文件。

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

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 
    } 
} 
+1

整齐而准确的答案:) – Keerthivasan

+0

@Octopus:谢谢 –

+0

非常感谢你的先生! =) – dens14345