【问题标题】:Insert image in resources folder Java在资源文件夹 Java 中插入图像
【发布时间】:2013-11-22 11:38:49
【问题描述】:

我有一个数据库WAMP,尽管有其他字段,我可以在其中插入名称和图像的扩展名,例如"imagen1.jpg"。 我正在做的程序是用JavaSQL 编写的句子,我正在使用Netbeans 的IDE 来做。 我有一个名为 "Imagenes.java" 的类型,我在其中选择图像的路径,通过这种方式,将其插入我的 JpanelJlabel

public class Imagenes extends javax.swing.JPanel {
    String path;
    ImageIcon imag;

    public Imagenes(int width,int height,String path)
    {    
        this.path=path;
        this.setSize(width,height);        
    }

    @Override
    public void paint(Graphics graph)
    {
        Dimension tam = getSize();
        if (imag!=null)
        graph.drawImage(imag.getImage(),0,0,tam.width, tam.height, null); 
        else
        {
            ImageIcon imagenFondo = new ImageIcon(getClass().getResource(path));        
            graph.drawImage(imagenFondo.getImage(),0,0,tam.width, tam.height, null);        
        }
        setOpaque(false);
        super.paintComponent(graph);
    }

}

我插入的所有图像都已保存在名为"resources" 的文件夹中。 因此,我想将我从"fileChooser" 中挑选的计算机中的图像添加到"resources" 文件夹中。 我已经以一千种方式尝试了它,放置了完整的路线,并且无法识别文件夹。然而,它 复制源文件夹中的图像。 我已经尝试过类似'\\resources\\imagen1.jpg',Java 特定的路由类型,但它没有正确选择它。但如果我这样做 打印路线的字符串,这是正确的。 我做错了什么?

【问题讨论】:

  • 尝试输入"src/resources/imagen1.jpg"
  • 但是我应该使用哪种方法将图像从我的计算机复制到资源文件夹?
  • 所以您只想将文件从一个目录复制到另一个目录?这是你唯一的问题吗?
  • 是的,我无法将它复制到资源文件夹,只能复制到源,当我编译并尝试它时,它说路径不存在,但是当我在控制台上打印它时,似乎正确。

标签: java image netbeans filechooser


【解决方案1】:

如果您使用ImageIcon(path) 而不是ImageIcon(getClass().getResource(path))。你可能想试试"src/resources/imagen1.jpg"。 Netbeans 将从 ProjectRoot 进行搜索。所以只要输入"resources/imagen1.jpg",就找不到了。您需要添加src

ProjectRootDir
            src
               resources
                      imagen1.jpg

当您使用ImageIcon(getClass().getResource(path)); 时,程序将尝试从classes 目录中的build 目录中定位图像。如果您将资源目录放在classes 目录中,使用ImageIcon(getClass().getResource(path)) 应该可以工作

ProjectRootDir
            build
                classes
                     resources
                             imagen1.jpg

在上述情况下,您的path 将是"resources/imagen.jpg"

【讨论】:

    【解决方案2】:

    我如何使用它来保存在资源文件夹中?

        // declare JFileChooser
    JFileChooser fileChooser = new JFileChooser();
    
    // let the user choose the destination file
    if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
        // indicates whether the user still wants to export the settings
        boolean doExport = true;
    
        // indicates whether to override an already existing file
        boolean overrideExistingFile = false;
    
        // get destination file
        File destinationFile = new File(fileChooser.getSelectedFile().getAbsolutePath());
    
        // check if file already exists
        while (doExport && destinationFile.exists() && !overrideExistingFile) {
            // let the user decide whether to override the existing file
            overrideExistingFile = (JOptionPane.showConfirmDialog(this, "Replace file?", "Export Settings", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION);
    
            // let the user choose another file if the existing file shall not be overridden
            if (!overrideExistingFile) {
                if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
                    // get new destination file
                    destinationFile = new File(fileChooser.getSelectedFile().getAbsolutePath());
                } else {
                    // seems like the user does not want to export the settings any longer
                    doExport = false;
                }
            }
        }
    
        // perform the actual export
        if (doExport) {
            // the code to write the file goes here...
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 2011-03-27
      • 1970-01-01
      • 2020-06-22
      • 2016-08-06
      • 1970-01-01
      相关资源
      最近更新 更多