将图像导入 Eclipse

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38046684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:13:41  来源:igfitidea点击:

Importing Images Into Eclipse

javaeclipseimageimportpaint

提问by HeroicO

I'm trying to import images into my eclipse project. Right now, to assign an image to a BufferedImage I'm using:

我正在尝试将图像导入到我的 eclipse 项目中。现在,要将图像分配给我正在使用的 BufferedImage:

try{
            player = ImageIO.read(new File(javax.swing.filechooser.FileSystemView.getFileSystemView().getHomeDirectory() + "\"));
        } catch (Exception e){
            e.printStackTrace();
        }

I have to get the path of the users Desktop, and the user of the game has to put the game and the images on their desktop, and it's just very annoying.

我必须得到用户桌面的路径,而游戏用户必须将游戏和图像放在他们的桌面上,这很烦人。

Question - How can I import an image into my eclipse project so I can just use:

问题 - 如何将图像导入到我的 eclipse 项目中,以便我可以使用:

new File("filename.png");

新文件(“文件名.png”);

and the image will export with the project jar, and still work?

并且图像将与项目 jar 一起导出,并且仍然有效?

Note - I'm tried this in the past by putting the image into the src folder or the Project folder, but I think I broke something in my Eclipse where it's not recognizing pngs as pngs.

注意 - 我过去曾尝试将图像放入 src 文件夹或 Project 文件夹中,但我想我在 Eclipse 中破坏了某些东西,它无法将 png 识别为 png。

回答by A.R

  1. From the main menu bar,
  2. select command link File > Import.... The Import wizard opens. Select General> File Systemand click Next.
  3. Click the Browsebutton on the next page of the wizard to select the directories from which you would like to add the resources.
  4. In the import selection panes, use the following methods to select exactly the resources you want to add:

    4.1 Expand the hierarchies in the left pane and select or clear the checkboxes that represent the folders in the selected directory. Then in the right pane, select or clear checkboxes for individual files.

    4.2 Click Filter Typesto filter the current selection for files of a specific type.

    4.3 Click SelectAll to select all resources in the directory, then go through and deselect the ones that you do not want to add.

    4.4 Click Deselect Allto deselect all resources in the directory, then go through and choose individual resources to add.

  5. Specify the Workbench project or folder that will be the import destination.

  6. When you have finished specifying your import options, click Finish.
  1. 从主菜单栏中,
  2. 选择命令链接文件 > 导入...。导入向导打开。选择“常规”>“文件系统”,然后单击“下一步”
  3. 单击向导下一页上的浏览按钮以选择要从中添加资源的目录。
  4. 在导入选择窗格中,使用以下方法准确选择要添加的资源:

    4.1 展开左窗格中的层次结构并选中或清除代表所选目录中文件夹的复选框。然后在右侧窗格中,选中或清除各个文件的复选框。

    4.2 单击过滤器类型过滤当前选择的特定类型的文件。

    4.3 单击“全”,选择目录中的所有资源,然后依次取消选择您不想添加的资源。

    4.4 点击取消全选,取消选择目录中的所有资源,然后依次选择要添加的单个资源。

  5. 指定将作为导入目标的 Workbench 项目或文件夹。

  6. 完成指定导入选项后,单击 完成

Reference: eclipse

参考:日食

回答by Bhavik Mehta

Just open Project->Build Project.Click on Resources.See the destination where you are making your project (for example by default it is workspace). Now place the image you want to import in bin folder of that project.

只需打开 Project->Build Project。单击 Resources。查看您制作项目的目的地(例如默认情况下它是工作区)。现在将要导入的图像放在该项目的 bin 文件夹中。