[英]How to add picture into JButton using JFilechooser
JFileChooser
无法正常工作,我在网上遇到了一个错误:
btnNewButton.setIcon(new ImageIcon(ImageIO.read(file)));
请帮助我,我是初学者。 它给了我这个错误:
线程“ AWT-EventQueue-0”中的异常java.lang.IllegalArgumentException:输入== null! 在javax.imageio.ImageIO.read(未知源)在RecordManagementSystem.addRecord $ 1.actionPerformed(addRecord.java:185)在javax.swing.AbstractButton.fireActionPerformed(未知源)在javax.swing.AbstractButton $ Handler.actionPerformed(未知) Source)javax.swing.DefaultButtonModel.fireActionPerformed(未知源)javax.swing.DefaultButtonModel.setPressed(未知源)javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知源)java.awt.Component.processMouseEvent (未知源)在javax.swing.JComponent.processMouseEvent(未知源)在java.awt.Component.processEvent(未知源)在java.awt.Container.processEvent(未知源)在java.awt.Component.dispatchEventImpl(未知Source)(java.awt.Container.dispatchEventImpl(未知源)at java.awt.Component.dispatchEvent(未知源)java.awt.LightweightDispatcher.retargetMouseEvent(未知源)java.awt.LightweightDispatcher.processMouseEvent(未知源)在java.awt.Lightw EightDispatcher.dispatchEvent(未知源),java.awt.Container.dispatchEventImpl(未知源),java.awt.Window.dispatchEventImpl(未知源),java.awt.Component.dispatchEventEvent(未知源),java.awt.EventQueue。 java.awt.EventQueue.access $ 000的dispatchEventImpl(未知源)java.awt.EventQueue $ 1.run的java.awt.EventQueue $ 1.run(未知源)的java.security.AccessController中的dispatchEvent(未知源) .doPrivileged(本机方法)(java.security.AccessControlContext $ 1.doIntersectionPrivilege(未知源)在java.security.AccessControlContext $ 1.doIntersectionPrivilege(未知源)在java.awt.EventQueue $ 2.run(未知源)在java.awt。 EventQueue $ 2.run(未知源)(java.security.AccessController.doPrivileged(本机方法)(java.security.AccessControlContext $ 1.doIntersectionPrivilege(未知源))(java.awt.EventQueue.dispatchEvent(未知源))。 java.awt.EventD上的EventDispatchThread.pumpOneEventForFilters(未知源) ispatchThread.pumpEventsForFilter(未知源),位于java.awt.EventDispatchThread.pumpEventsForHierarchy(未知源),位于java.awt.EventDispatchThread.pumpEvents(未知源),位于java.awt.EventDispatchThread.pumpEvents(未知源),位于java.awt.EventDispatchThread。运行(未知源)
码:
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);
}
}
}
});
在选择文件之前 ,您是从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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.