我目前正在學習 Java,但遇到了問題,找不到任何相關資訊。我收到一個“javax.imageio.IIOException:無法讀取輸入檔案!” 我不明白我做錯了什么:這是我的代碼:
public class FirstWindoww extends JFrame {
JTextField usernameText = new JTextField(30);
private JPanel panel1;
private JPanel panel2;
public FirstWindoww() {
super("FROGGER GAME");
JFrame frame = this;
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setSize(400, 400);
this.setLayout(new FlowLayout());
panel1 = (JPanel) this.getContentPane();
panel1.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));
panel1.setLayout(new FlowLayout());
panel2 = (JPanel) this.getContentPane();
panel1.setBackground(Color.GREEN);
JPanel startingGame = new JPanel();
this.setVisible(true);
JLabel username = new JLabel("ENTER YOUR NAME");
panel1.add(username);
panel1.add(usernameText);
usernameText.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Bouton cliqué !");
}
});
JButton start = new JButton("START");
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.remove(panel1);
frame.add(panel2);
frame.validate();
}
});
panel1.add(start);
JButton exit = new JButton("EXIT");
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
panel1.add(exit);
JButton help = new JButton("HELP");
help.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
panel1.add(help);
try {
// Load the background image
BufferedImage img = ImageIO.read(new File("C:\\Users\\N\\Documents\\testGUI\\srce\\grenouille"));
this.setContentPane(new JLabel(new ImageIcon(img)));
} catch (IOException exp) {
exp.printStackTrace();
}
JLabel imageLabel = new JLabel();
imageLabel.setIcon(new ImageIcon("C:\\Users\\N\\Documents\\testGUI\\src\\grenouille.png"));
panel1.add(imageLabel);
}
這是圖片中的錯誤,您可以看到右側的欄位 在此處輸入圖片說明
感謝您抽出時間來閱讀。祝大家有美好的一天:) !!!
uj5u.com熱心網友回復:
你確定你也有正確的路徑嗎?
BufferedImage img = ImageIO.read(new File("C:\\Users\\N\\Documents\\testGUI\\srce\\grenouille"));
和
imageLabel.setIcon(new ImageIcon("C:\\Users\\N\\Documents\\testGUI\\src\\grenouille.png"));
uj5u.com熱心網友回復:
不要使用絕對路徑,例如C:\Users\N\Documents\testGUI\src\grenouille,如果您移動程式/影像的位置,這些將導致無休止的問題。
另外,看著你的路徑,C:\Users\N\Documents\testGUI\srce\grenouille你似乎在src(即srce)中有一個錯字,并且你錯過了檔案擴展名grenouille
嘗試處理這些問題時的一個提示是檢查 的結果File#exits,這將使您快速指示可能不正確的路徑橫向。
相反,您應該專注于使用“嵌入式資源”,這允許資源與二進制檔案捆綁在一起,并且它們在運行時變得更容易訪問。
執行此操作的確切方法取決于您使用的 IDE 以及用于構建和打包專案的作業流程。
對于基于 Ant 的 Netbeans 專案(我相信 Eclipse 也是如此),您可以將資源直接放在src專案的檔案夾下。然后,當您匯出專案時,構建系統會將其包含在生成的 Jar 檔案中。對于基于 Maven 的專案,它涉及的更多一些,我不打算在這里討論它。
因此,一旦您“嵌入”了資源,您就可以簡單地使用Class#getResource(或Class#getResourceAsStream根據您的需要),例如......
BufferedImage img = ImageIO.read(getClass().getResource("/grenouille.png")));
資源的位置是相對于類路徑的頂部/根目錄,為了簡單起見,將其視為距src目錄頂部的偏移量(不包括src)
如果您繼續遇到問題,您將不得不在最后診斷問題。首先執行清理/構建(或專案的匯出)并解壓縮生成的 Jar 檔案并檢查其內容。如果資源不存在,要么是專案配置不正確,要么是資源位置錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/374387.html
標籤:爪哇 摇摆 控制板 框架 javax.imageio
上一篇:在線繪制具有特定角度度數的字串
