我想在Swing中做一件事,希望我足够清楚。
我想显示带有图标的文件列表,用户已根据扩展名与该特定文件相关联。但是,我希望在程序中生成这个文件列表 - 我的意思是:显示的文件图标不是是文件夹中的实际文件(所以我不能使用 JFileChooser
)。
有什么可以帮助我的吗?
最佳答案
嗨,我在谷歌搜索时遇到了这个问题......跳![alt text][1]e 这有帮助:)
import java.io.*;
import javax.swing.*;
public class IconExtract1 {
public static void main(String[] args) throws Exception {
String s = "c:/windows/regedit.exe";
File file = new File(s);
// Get metadata and create an icon
sun.awt.shell.ShellFolder sf =
sun.awt.shell.ShellFolder.getShellFolder(file);
Icon icon = new ImageIcon(sf.getIcon(true));
System.out.println("type = " + sf.getFolderType());
// show the icon
JLabel ficon = new JLabel(s, icon, SwingConstants.LEFT);
JFrame frame = new JFrame();
frame.getContentPane().add(ficon);
frame.pack();
frame.setVisible(true);
}
}
另一种方式:
import java.io.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.filechooser.FileSystemView;
public class IconExtract2 {
public static void main(String[] args) throws Exception {
String s = "c:/windows/regedit.exe";
File file = new File(s);
// Get metadata and create an icon
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(file);
// show the icon
JLabel ficon = new JLabel(s, icon, SwingConstants.LEFT);
JFrame frame = new JFrame();
frame.getContentPane().add(ficon);
frame.pack();
frame.setVisible(true);
}
}
关于java - 在 swing 中显示生成的文件列表中的文件图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3481862/