1

我的程序包含两个文件:

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子目录中。

4

1 回答 1

0

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

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

于 2013-09-10T17:14:38.317 回答