复制代码代码如下:
导入java.io.File;
导入java.io.IOException;
导入 java.awt.BorderLayout;
导入 java.awt.event.ActionEvent;
导入 java.awt.event.ActionListener;
导入 javax.swing.Icon;
导入 javax.swing.ImageIcon;
导入 javax.swing.JButton;
导入 javax.swing.JFrame;
导入javax.swing.JPanel;
导入 javax.swing.JTextField;
导入 javax.swing.JLabel;
导入 javax.swing.BoxLayout;
导入 javax.swing.filechooser.FileSystemView;
导入 sun.awt.shell.ShellFolder;
公共类 FileIconExtractor 扩展 JFrame 实现 ActionListener{
private JButton getIconBtn = new JButton("获取图标");
私有 JPanel iconPanel = new JPanel();
私有 JTextField extField = new JTextField();
private JLabelsmallIconLabel = new JLabel("这里是小图标");
private JLabel bigIconLabel = new JLabel("此处为大图标");
公共文件图标提取器(){
this.setSize(200, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
getIconBtn.setActionCommand("GETICON");
getIconBtn.addActionListener(this);
iconPanel.setLayout(new BoxLayout(iconPanel, BoxLayout.Y_AXIS));
iconPanel.add(smallIconLabel);
iconPanel.add(bigIconLabel);
this.add(extField, BorderLayout.NORTH);
this.add(iconPanel, BorderLayout.CENTER);
this.add(getIconBtn, BorderLayout.SOUTH);
this.setVisible(true);
}
公共无效actionPerformed(ActionEvent e){
if (e.getActionCommand().equals("GETICON")) {
字符串 ext = extField.getText();
文件档案;
尝试
{
file = File.createTempFile("icon", "." + ext);
FileSystemView 视图 = FileSystemView.getFileSystemView();
图标smallIcon = view.getSystemIcon(file);
ShellFolder shellFolder = ShellFolder.getShellFolder(文件);
图标 bigIcon = new ImageIcon(shellFolder.getIcon(true));
setIconLabel(小图标, 大图标);
文件.删除();
}
捕获(IOException ioe)
{
ioe.printStackTrace();
}
}
}
私人无效 setIconLabel(图标小图标,图标大图标){
小图标标签.setIcon(小图标);
bigIconLabel.setIcon(bigIcon);
}
公共静态无效主(字符串[] args){
FileIconExtractor fie = new FileIconExtractor();
}
}