简体   繁体   English

从eclipse的资源文件夹中读取图像,而不使用图像名称

[英]Read images from resource folder of eclipse without using Image name

I want to read Images from resource folder of eclipse without using image name. 我想从Eclipse的资源文件夹中读取图像而不使用图像名称。 As of now I am reading it from current directory and taking Absolute Path (photoPath) so that I can reproduce the Image in other panel of a JFrame. 到目前为止,我正在从当前目录中读取它,并使用“绝对路径”(photoPath),以便可以在JFrame的其他面板中重现Image。 However if I make executable jar its start taking images from current directory, my image folder is not coming. 但是,如果我使可执行jar开始从当前目录中获取图像,则不会出现我的图像文件夹。 Here is my Image choosing code 这是我的图片选择代码

    imageChooser = new JButton("ImageChooser");
    panel1.add(imageChooser);
    fc = new JFileChooser();
    imageLabel = new JLabel();
    imageChooser.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            fc.setCurrentDirectory(new File("."));
            int result = fc.showOpenDialog(imageChooser);

            if (result == JFileChooser.APPROVE_OPTION) {

                currentFile = fc.getSelectedFile();
                imageLabel.setIcon(new ImageIcon(currentFile.toString()));
                panel1.add(imageLabel);
                panel1.validate();
                photoName=fc.getSelectedFile().getName();
                photoPath = currentFile.getAbsolutePath();
            }
        }
    });

Here is my structure 这是我的结构

在此处输入图片说明

use .getResource() 使用.getResource()

Example: 例:

new ImageIcon(getClass().getResource("/images/button.png"))

This will work for JAR as well. 这也将适用于JAR。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM