亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本文介紹了打開<<錯誤javax.Imageio.IIO異常:當(dāng)我嘗試在JPanel的背景上添加圖像時,無法讀取輸入文件!&>的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我目前正在學(xué)習(xí)Java,我有問題,找不到任何關(guān)于它的東西。我收到了一個javax.Imageio.IIO異常:無法讀取輸入文件!&q;
我不明白我做錯了什么:
這是我的代碼:

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);

    }

以下是圖片中的錯誤,您可以在右側(cè)看到字段enter image description here

感謝您抽出時間閱讀本文。祝大家有愉快的一天:)!

推薦答案

不要使用絕對路徑,如C:UsersNDocuments estGUIsrcgrenouille,如果您移動程序/圖像的位置,這些路徑將導(dǎo)致無休止的問題。

另外,查看您的路徑C:UsersNDocuments estGUIsrcegrenouille,您似乎在src(即srce)中有一個拼寫錯誤,并且您缺少grenouille

中的文件擴展名

嘗試處理這些問題時的一個提示是檢查File#exits的結(jié)果,這將為您提供可能不正確的路徑分支的快速提示。

相反,您應(yīng)該專注于使用"嵌入式資源,這允許資源與二進制文件捆綁在一起,并且在運行時可以更輕松地訪問它們。

執(zhí)行此操作的確切方法取決于您正在使用的IDE以及用于生成和打包項目的工作流。

對于基于Ant的NetBeans項目(我相信也是),您可以將資源直接放在項目的src文件夾下。然后,當(dāng)您導(dǎo)出項目時,構(gòu)建系統(tǒng)會將其包含在生成的Jar文件中。對于基于Maven的項目,它稍微復(fù)雜一些,我不打算在這里討論它。

因此,一旦您嵌入了資源,您只需使用Class#getResource(或根據(jù)您的需要使用Class#getResourceAsStream),例如…

緩沖圖像img=ImageIO.read(getClass().getResource("/grenouille.png")));

資源的位置相對于類路徑的頂部/根,為了簡單起見,可以將其視為相對于src目錄頂部的偏移量(不包括src)

如果您繼續(xù)有問題,您將不得不在您的一端診斷問題。首先進行一次干凈的/構(gòu)建(或項目的導(dǎo)出),然后解壓縮得到的Jar文件并檢查其內(nèi)容。如果資源不存在,則說明項目配置不正確或資源位于錯誤的位置。

這篇關(guān)于打開<<錯誤javax.Imageio.IIO異常:當(dāng)我嘗試在JPanel的背景上添加圖像時,無法讀取輸入文件!&>的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標(biāo)簽:圖像 嘗試 異常 當(dāng)我 背景 讀取 輸入
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定