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

时间:2023-01-24 16:53:31

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. The program worked fine in DrJava but I transferred to eclipse in order to make a jar file.

我有一个程序运行并没有在eclipse中抛出运行时错误,应该在程序结束时将图像设置为JButton作为结果,但图像永远不会放在按钮上。该程序在DrJava中运行良好但我转移到eclipse以生成jar文件。

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目录,但它没有解释如何实际修复问题...我是新的日食,所以如果有人可以帮助我out id感激不尽。

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

1 个解决方案

#1


0  

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". 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. And if you generate a jhar for your app, they will be bundled in the jar with your classes. Once this is done, you just have to use the constructor taking a URL as argument, and pass

可以从Eclipse中的运行配置更改此目录,在“参数”选项卡下。但这可能不是你想要的。图标应该与您的应用程序捆绑在一起,并加载类加载器。将它们放在源文件夹中的包下。 Eclipse会将它们与已编译的类一起复制到输出目录。如果你为你的应用程序生成一个jhar,它们将与你的类一起捆绑在jar中。完成此操作后,您只需使用构造函数将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.

这将允许您轻松地将图标与应用程序捆绑在一起,并且无论工作目录是什么,都会加载图标。

#1


0  

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". 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. And if you generate a jhar for your app, they will be bundled in the jar with your classes. Once this is done, you just have to use the constructor taking a URL as argument, and pass

可以从Eclipse中的运行配置更改此目录,在“参数”选项卡下。但这可能不是你想要的。图标应该与您的应用程序捆绑在一起,并加载类加载器。将它们放在源文件夹中的包下。 Eclipse会将它们与已编译的类一起复制到输出目录。如果你为你的应用程序生成一个jhar,它们将与你的类一起捆绑在jar中。完成此操作后,您只需使用构造函数将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.

这将允许您轻松地将图标与应用程序捆绑在一起,并且无论工作目录是什么,都会加载图标。