简体   繁体   English

如何在Eclipse中将图像添加到项目目录?

[英]how do you add images to the project directory in eclipse?

I have a program that runs and doesnt throw a runtime error in eclipse which should set up an image to a JButton at the end of the program as the result but the image is never put on to the button. 我有一个程序可以运行,并且不会在Eclipse中抛出运行时错误,因此应该在程序结束时将图像设置到JButton上,但图像永远不会放在按钮上。 The program worked fine in DrJava but I transferred to eclipse in order to make a jar file. 该程序在DrJava中运行良好,但为了制作一个jar文件,我转到了eclipse。

I saw in another question posted that someone had a similar problem that said the images should be put into the project directory not the src directory but it didnt explain how to actually fix the problem... im new to eclipse so if someone could help me out id appreciate it thanks. 我在另一个发布的问题中看到有人存在类似的问题,即应将图像放入项目目录而不是src目录,但它没有解释如何真正解决问题……日食是新事物,所以如果有人可以帮助我出了身份证,谢谢。

here is how the images are set up in my code: 这是在我的代码中如何设置图像:

public void tempSelection70 (int fRate, int wbTemp, int rcTons, int a, int r)
  {
    for (int model = 0; model < 7; model++)
    {
      MyArray y = new MyArray(tons70FCharts[model], "t");
      int[][] x = y.getArray();
      int t = x[a][r];
      if (rcTons == t)
      {
        tableButton = new JButton(new ImageIcon(tablesFor70[model], tablesFor70[model]));
        break;
      }
      else 
      {
        tableButton = new JButton(new ImageIcon("CANNOT_FIND_MODEL.GIF", "SCROLL"));
      }
    }
    for (int model = 0; model < 7; model++)
    {
      MyArray y = new MyArray(flow70FCharts[model], "f");
      int[][] x = y.getArray();
      int t = x[a][r];
      if (fRate == t)
      {
        tableButton = new JButton(new ImageIcon(tablesFor70[model], tablesFor70[model]));
        break;
      }
      else 
      {
        tableButton = new JButton(new ImageIcon("CANNOT_FIND_MODEL.GIF", "SCROLL"));
      }
    }
  }

You're passing a file name as argument. 您正在传递文件名作为参数。 I guess this file name is a relative file name. 我猜这个文件名是一个相对文件名。 So the file is relative to the directory from which your IDE launches the java command to execute your application: the working directory. 因此,该文件相对于您的IDE从中启动java命令以执行您的应用程序的目录:工作目录。

This directory can be changed from the run configuration in Eclipse, under the tab "Arguments". 可以在Eclipse的运行配置中的“参数”选项卡下更改此目录。 But this is probably not what you want. 但这可能不是您想要的。 The icons should certainly be bundled with your application, and loaded with the class loader. 这些图标当然应该与您的应用程序捆绑在一起,并与类加载器一起加载。 Put them under a package in your source folder. 将它们放在源文件夹中的包装下。 Eclipse will copy them to the output directory, along with the compiled classes. Eclipse会将它们以及已编译的类复制到输出目录。 And if you generate a jhar for your app, they will be bundled in the jar with your classes. 而且,如果您为应用程序生成了一个jhar,它们将与您的课程一起捆绑在jar中。 Once this is done, you just have to use the constructor taking a URL as argument, and pass 完成此操作后,您只需使用以URL作为参数的构造函数,然后传递

SomeClassOfYourApp.getResource("/the/package/of/the/image.png")

as this URL argument. 作为此URL参数。

This will allow you to bundle the icons with the app easily, and will load the icons whatever the working directory is. 这将使您可以轻松地将图标与应用程序捆绑在一起,并且无论工作目录是什么,都将加载图标。

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

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