當執行我的代碼,有一個錯誤的問題與此代碼:在java中顯示圖像時出錯。 AWT-EventQueue的 - 0錯誤
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String path = "";
String extension = "";
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
path = selectedFile.getAbsolutePath();
int i = path.lastIndexOf('.');
if (i >= 0) {
extension = path.substring(i+1);
}
}
try {
if (extension.equals("ppm")) {
Paint.imgs.add(new ppm(path));
img.setImage(resize(Paint.imgs.get(Integer.parseInt(path)).img, 544, 372));
img.repaint();
}
} catch (IOException ex) {
Logger.getLogger(getName()).log(Level.SEVERE, null, ex);
}
}
});
與線
img.setImage(resize(Paint.imgs.get(Integer.parseInt(path)).img, 544, 372));
它看起來像路徑圖像是正確的
特別,所以我不知道爲什麼它不起作用。 輸出是:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "C:\Users\ciola\Desktop\pb.ppm"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at paint.binar$1.actionPerformed(binar.java:435)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
...
任何想法如何解決這個問題?
您在路徑上調用Integer.parseInt是錯誤的。這絕對是錯誤的 – ControlAltDel
@ControlAltDel,然後,它不起作用。紅色下劃線。 –
這是因爲您正在嘗試引用數組中的某些東西。我不知道你期望如何從文件路徑轉到數字索引 – ControlAltDel