getResource和getResourceAsStream返回null

时间:2013-09-10 17:08:29

标签: java resources

我的程序包含两个文件:

package net.sf.junace.demo;

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.swing.*;
import javax.swing.plaf.metal.MetalIconFactory;
import javax.swing.table.DefaultTableModel;

public class Manager extends JFrame {
    private static final long serialVersionUID = 1L;
    private JTextField textField;
    private JPanel panel;
    private JScrollPane scrollPanel;
    private JTable table;

    public Manager(String string) {
        super(string);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel = new JPanel();
        panel.setLayout(new BorderLayout(2, 2));

        textField = new JTextField(10);
        panel.add(textField, BorderLayout.NORTH);
        String[] columnNames = { "Name", "Size", "Modified" };
        File[] filesAndDirs = new File(".").listFiles();
        Object[][] data = new Object[filesAndDirs.length][3];
        for (int i = 0; i < filesAndDirs.length; i++) {
            data[i][0] = filesAndDirs[i].getName();
            data[i][1] = filesAndDirs[i].length();
            Date date = new Date(filesAndDirs[i].lastModified());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
            data[i][2] = sdf.format(date);
        }
        table = new JTable(data, columnNames);

        table.getTableHeader().getColumnModel().getColumn(0)
                .setCellRenderer(new IconRenderer(filesAndDirs));
        scrollPanel = new JScrollPane(table);
        panel.add(scrollPanel, BorderLayout.CENTER);
        add(panel);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    protected static void createAndShowGUI() {
        Manager frame = new Manager("Manager");
    }
}

和第二:

package net.sf.junace.demo;

import java.awt.Component;
import java.io.File;
import java.io.InputStream;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;

public class IconRenderer extends DefaultTableCellRenderer {
    File[] files;
    // URL url;
    ImageIcon folderIcon;

    public IconRenderer(File[] files) {
        super();
        this.files = files;
        // THIS not works
        // InputStream in =
        // getClass().getClassLoader().getResourceAsStream("/folder16.png");
        // URL url = getClass().getClassLoader().getResource("/folder16.png");
        // folderIcon = new ImageIcon(url);

        // THIS works fine
        folderIcon = new ImageIcon("d:\\code\\proj.git\\Ikony\\folder16.png");

    }

    private static final long serialVersionUID = 1L;

    public Component getTableCellRendererComponent(JTable table, Object obj,
            boolean isSelected, boolean hasFocus, int row, int column) {
        super.getTableCellRendererComponent(table, obj, isSelected, hasFocus,
                row, column);
        if (files[row].isDirectory())
            setIcon(folderIcon);
        setBorder(UIManager.getBorder("TableHeader.cellBorder"));
        return this;
    }
}

此节目列出文件和目录。目录必须有图标。此示例适用于目录:

folderIcon = new ImageIcon("d:\\code\\proj.git\\Ikony\\folder16.png");

但我想在项目目录中放置文件folder16.png,将来放在.jar中。不起作用getResource和getResourceAsStream:

InputStream in = getClass().getClassLoader().getResourceAsStream("/folder16.png");

返回null

URL url = getClass().getClassLoader().getResource("/folder16.png");

还会返回null,无论我将folder16.png放在哪里:在根项目目录中或src子目录中。

1 个答案:

答案 0 :(得分:0)

您应该将它与需要该文件的类的Java源文件放在一起。然后,您应该使用Class.getResource(String name)Class.getResourceAsStream(String name)。换句话说,您应该跳过getClassLoader()来电。

您不需要文件的完整路径名,它会通过上述方法自动添加到名称前面。最好使用IconRenderer.class而不是getClass(),因为不需要在运行时确定类。如果你这样做,那么扩展你的类的类可能会失败。