打开<<错误javax.Imageio.IIO异常:当我尝试在JPanel的背景上添加图像时,无法读取输入文件!&> [英] Got on <<error javax.imageio.IIOException: Can't read input file!>> when I tried to add an image on the background of my JPanel

查看:0
本文介绍了打开<<错误javax.Imageio.IIO异常:当我尝试在JPanel的背景上添加图像时,无法读取输入文件!&>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习Java,我有问题,找不到任何关于它的东西。我收到了一个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);

    }

以下是图片中的错误,您可以在右侧看到字段enter image description here

感谢您抽出时间阅读本文。祝大家有愉快的一天:)!

推荐答案

不要使用绝对路径,如C:UsersNDocuments estGUIsrcgrenouille,如果您移动程序/图像的位置,这些路径将导致无休止的问题。

另外,查看您的路径C:UsersNDocuments estGUIsrcegrenouille,您似乎在src(即srce)中有一个拼写错误,并且您缺少grenouille

中的文件扩展名

尝试处理这些问题时的一个提示是检查File#exits的结果,这将为您提供可能不正确的路径分支的快速提示。

相反,您应该专注于使用";嵌入式资源,这允许资源与二进制文件捆绑在一起,并且在运行时可以更轻松地访问它们。

执行此操作的确切方法取决于您正在使用的IDE以及用于生成和打包项目的工作流。

对于基于Ant的NetBeans项目(我相信也是),您可以将资源直接放在项目的src文件夹下。然后,当您导出项目时,构建系统会将其包含在生成的Jar文件中。对于基于Maven的项目,它稍微复杂一些,我不打算在这里讨论它。

因此,一旦您嵌入了资源,您只需使用Class#getResource(或根据您的需要使用Class#getResourceAsStream),例如...

缓冲图像img=ImageIO.read(getClass().getResource(";/grenouille.png";)));

资源的位置相对于类路径的顶部/根,为了简单起见,可以将其视为相对于src目录顶部的偏移量(不包括src)

如果您继续有问题,您将不得不在您的一端诊断问题。首先进行一次干净的/构建(或项目的导出),然后解压缩得到的Jar文件并检查其内容。如果资源不存在,则说明项目配置不正确或资源位于错误的位置。

这篇关于打开<<错误javax.Imageio.IIO异常:当我尝试在JPanel的背景上添加图像时,无法读取输入文件!&>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆