我目前正在尝试将刽子手图像添加到我的JPanel
. 我已标记图像 1-10 并在每次用户得到错误答案时增加值(这是有效的)。到目前为止,我还无法使图像正常工作。如何正确插入我的图像?
if(!found){
numError++;
String userError = Integer.toString(numError);
String jpg = userError.concat(".JPG");
try{
BufferedImage myImg = ImageIO.read(new File(jpg));
JLabel hangman = new JLabel(new ImageIcon(myImg));
hangman.setSize(200,100);
hangman.setLocation(300, 20);
add(hangman);
}catch(IOException ex){
ex.printStackTrace();
}
}
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at HangmanPanel$1.actionPerformed(HangmanPanel.java:73)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
...